[HowTo] Find Keyword In Files With Grep In Ubuntu

Most of the time when you want to debug your code or someone code which separates in many files, you need to know or locate where this function is defined or where this function called. You will waste so much of your time when you open your files one by one. So what is the solution then?

Most of Linux distribution will have “grep” function installed. You can use grep to find a pattern of string contain 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 use this command:

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

Here is the definition:
grep is 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 lot of your time to debug 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 to here and here to see it in many examples.

Comments

  1. Laiq says:

    Thanks. grep to find files with given keyword was useful.

  2. voku says:

    "find + grep" is faster then "grep -R"…

    find /home/ivan/myscripts/ -type f -exec grep -in "my_defined_function" {} ;

  3. vivek says:

    thanks buddy

Give me your feedback

This site uses Akismet to reduce spam. Learn how your comment data is processed.