7

I am looking for a specific phrase in CentOS and I am not sure where and which file or even directory that has the file is. How can I do a complete recursive search on a phrase. Thanks

Bart Silverstrim
  • 31,172
  • 9
  • 67
  • 87
user50946
  • 483
  • 2
  • 7
  • 18

3 Answers3

16

man grep
For this example, grep -i -R "your phrase" directory
-i means case insensitive
-R means recursively

If you don't know which directory, use / - but be prepared for it to take a long time.

James L
  • 6,025
  • 1
  • 22
  • 26
  • as I said I have no idea about directory – user50946 Sep 24 '10 at 13:45
  • updated with the root directory. It will take a long time though. – James L Sep 24 '10 at 13:46
  • I did what you said but there are a lots of records..is there any way to save this to text file? – user50946 Sep 24 '10 at 13:49
  • 3
    read about piping and redirects. > saves to a file, >> appends to a file, and | pipes to a process. So, in this case, `grep -i -R "your phrase" directory > /path/to/your/textfile` - also consider adding -l to the grep arguments if you just want a list of the files (as per @Richard Holloway – James L Sep 24 '10 at 13:50
  • 1
    you can redirect the output to a file with `grep -i -R "your phrase" directory > myfile.txt`, or you could use less to view the result with `grep -i -R "your phrase" directory | less` – Avada Kedavra Sep 24 '10 at 13:52
  • Make sure the file you are redirecting your output to isn't in the search path, or grep will find its own output. Recursion is only fun the first million times. – jmanning2k Sep 24 '10 at 17:19
4

You can use this one liner to get a list of all files in this folder and sub folders, containing the phrase "The phrase I am looking for".

  find . -print0 | xargs -0 grep "The phrase I am looking for" -l
Richard Holloway
  • 7,456
  • 2
  • 25
  • 30
2

I would recommend ack.

Go to the uppermost level directory that you dare searching and then type:

ack -a "Phrase"

I don't really use grep anymore because of ack.

  • ack is great. You can install it on CentOS from the rpmforge repo. See http://rpmrepo.org/RPMforge/Using for details on how to set this up. – Richard Holloway Sep 24 '10 at 19:52