How to search recursively using grep

Asked

Viewed 7,529 times

9

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

I tried to travel with:

find . | grep "palavra"

5 answers

9


Use the parameter -r of grep.

grep -r "foo" . 

Where the . 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 be similar there is this:

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

 /home/user/file1.txt:foo
 /home/user/file2.txt:foo
 /home/user/file3.txt:foo
 ~$
  • I used so --------- grep -nRHI "word" *

  • Thank you, understand @Thiago

4

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

find . -exec grep 'palavra' {} \;

2

I still think the best thing could be

grep-ir "word" .

to search for any case sensitive occurrence

  • you meant case-insensitive, right?

  • probably heheheh

1

I hope this helps: grep -R "Word" ./

  • It would be interesting to explain your answer, to facilitate understanding of how this helps to solve the problem

0

Just complementing the other answers, I think it is worth explaining why your command did not work (besides giving another alternative that was not mentioned).

First, about the command:

find . | grep "palavra"

What the pipe (the |) is to take the output of the first command and pass as input to the second. How find . brings the listing of all files and directories (starting from the current directory), so in fact the grep is searching for "word" in names of these files and directories. That is, if you have a file or directory whose name contains "word", it will be returned.

So that the output of the command find . be passed as arguments to the grep (and this search the contents of the file), you can use the option -exec, as already suggested in another answer, or else use xargs:

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

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

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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.