How to search recursively using grep

7

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

I tried to navigate with:

find . | grep "palavra"
    
asked by anonymous 26.03.2014 / 14:17

4 answers

6

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 similar to this:

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

 /home/user/file1.txt:foo
 /home/user/file2.txt:foo
 /home/user/file3.txt:foo
 ~$
    
26.03.2014 / 14:21
3

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

find . -exec grep 'palavra' {} \;
    
26.03.2014 / 19:46
2

I still think the best could be the

grep -ir "word".

to search for any case sensitive instance

    
26.03.2014 / 14:59
1

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

    
10.05.2017 / 09:39