How to search recursively using grep

How to search for a certain word recursively in all files of the current directory and its subdirectories?

I tried traversing with:

find . | grep "palavra"
Author: eightShirt, 2014-03-26

5 answers

Use the -r parameter of grep.

grep -r "foo" . 

Where . indicates the current directory to start the search.

If you prefer to search for a word in files with a specific extension, you can do:

grep -r "foo" ~/*.txt

To ignore warning messages use parameter -s or --no-messages:

grep -r -s "foo" ~/*.txt

The output will look like this:

 ~$ grep -r -s "foo" ~/*.txt

 /home/user/file1.txt:foo
 /home/user/file2.txt:foo
 /home/user/file3.txt:foo
 ~$
 9
Author: stderr, 2016-10-14 01:08:13

You can also use the -exec option of find along with grep. If there is a need to filter the files with find the option below will be faster than grep -r, because it will not try to search for the" word " in all files:

find . -exec grep 'palavra' {} \;
 4
Author: Bruno Coimbra, 2014-03-26 18:46:10

I still think the best could be

Grep-go "word".

To search for any case sensitive occurrence

 2
Author: Otto, 2014-03-26 13:59:55

Hope this helps: grep-R "word"./

 1
Author: Momede Hassanigy, 2017-05-10 07:39:45

Just complementing the other answers, I think it's worth explaining why your command didn't work (besides giving another alternative that wasn't mentioned).

First, about the command:

find . | grep "palavra"

What the pipe (the |) does is take the output of the first command and pass it as input to the second. Since find . brings up the list of all files and directories (starting from the current directory), then in fact grep is doing the search for" word " in the names of these files and directories. That is, if you have any file or directory whose name contains "word", it will be returned.

For the output of the find . Command to be passed as arguments to the grep (and it searches the contents of the file), you can use the -exec option, as already suggested in another answer, or use xargs:

find . -type f -print0 | xargs -0 grep "palavra"

xargs takes the output of find (i.e. the filenames) and passes them as parameters for grep, so the search for "word" will be done in the contents of these files (and no longer in the name).

The -type f option searches only for the files, ignoring the directories (because grep gives error if you pass a directory as a parameter).

The -print0 option prints the file names with the NULL CHARACTER between them (instead of printing one per line, as is the default of find). And the -0 option causes xargs to use or NULL character as a separator. Thus, if the filenames have spaces, new lines , quotes, etc., these will be interpreted and passed correctly to grep (without this option, many filenames with the already mentioned characters can be passed incorrectly and grep will give error).

One difference to another answer is that -exec executes the command multiple times (once for each file found), while xargs groups several grep (including, you can control how many files are passed at a time, with --max-args, or how many characters can be passed at each execution, with --max-chars - see the documentation for more options).

 0
Author: hkotsubo, 2020-04-24 15:42:10