I like the unix find
command but I always find it too 'fiddly' to use when I want to search through my project looking for a piece of text in any file in any directory or subdirectory.
Is there an easier way to do this?
Try grep
utility:
use: grep -rl alvin .
Your recursive grep
searches don't have to be limited to just the current directory. This next example shows how to recursively search two unrelated directories for the case-insensitive string "alvin":
grep -ril alvin /home/cato /htdocs/zenf
You can also perform recursive searches with the egrep
command, which lets you search for multiple patterns at one time.
egrep -ril 'aja|alvin' .
Note that in this case, quotes are required around the search pattern.
Summary: grep -r notes:
A few notes about the grep -r
command:
This grep command doesn't make much sense unless you use it with the -l (lowercase "L") flag as well. This flag tells grep to print the matching filenames.
Don't forget to list one or more directories at the end of your grep command. If you forget to add any directories, grep will attempt to read from standard input (as usual).
As shown, you can use other normal grep flags as well, including -i to ignore case, -v to reverse the meaning of the search, etc.