[HowTo] Find Keyword In Files With Grep In Ubuntu

Most of the time when you want to debug your code or someone else’s code which is spread across many files, you need to know or locate where a function is defined or where it is called. You will waste so much of your time when you open your files one by one. So what is the solution?

Most Linux distributions will have the “grep” command installed. You can use grep to find a pattern of string contained in files. Let’s say you want to find a function named “my_defined_function” in folder /home/ivan/myscripts/, you can find it by using this command:

grep -i -n -r 'my_defined_function' /home/ivan/myscripts/

Here is the definition:
grep is the function name to search a pattern in files
-i means ignore case
-n means output line number
-r search recursively

It’s easy, isn’t it? You can save a lot of your time when debugging your script. You can use grep in so many ways. You can type:

grep --help

or

man grep

to see all the arguments and how to use it. And you can go here and here to see it in many examples.