How do I find a word recursively in all files and all directories
23,958
Solution 1
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.
Solution 2
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
Solution 3
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
.
Related videos on Youtube
Author by
user50946
Updated on September 17, 2022Comments
-
user50946 over 1 year
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
-
user50946 over 13 yearsas I said I have no idea about directory
-
James L over 13 yearsupdated with the root directory. It will take a long time though.
-
user50946 over 13 yearsI did what you said but there are a lots of records..is there any way to save this to text file?
-
James L over 13 yearsread 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 over 13 yearsDoes this add any functionality over grep's recursive flag?
-
Femil Shajin over 13 yearsyou 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 withgrep -i -R "your phrase" directory | less
-
pauska over 13 yearsI'm also a pipe abuser. Once you're used to it it's hard to stop!
-
Richard Holloway over 13 yearsMy servers cost enough money and do so little in return, what is a few extra processes here and there? :)
-
Richard Holloway over 13 yearsack is great. You can install it on CentOS from the rpmforge repo. See rpmrepo.org/RPMforge/Using for details on how to set this up.