Is there a simpler way to grep all files under a directory?
Check if your grep
supports -r
option (for recurse):
grep -r <search_string> .
If you want to recurse down into subdirectories:
grep -R 'pattern' .
The -R
option is not a standard option, but is supported by most common grep
implementations.
A sub optimal answer :
Instead of piping the output of find
into grep
, you could just run
find . -type f -exec grep 'research' {} '+'
and voila, one command instead of two !
explanation :
find . -type f
find all regular files within .
-exec grep 'research'
grep 'research'
{}
in found filename
'+'
use one command per all the filenames, not once per filename.
Nb : with ';'
it would have been once per filename.
Other than that, if you use that to process source code, you may look into ack
, which is made for looking for code bits easily.
ack
Edit :
You can extend that research a little. First, you can use the -name ''
switch of find
to look for files with specifig naming pattern.
For instance :
only files that correspond to logs :
-name '*.log'
only files that correspond to c headers, but you can't stick with uppercase or lowercase for your filename extensions :
-iname *.c
Nb : like for grep
and ack
, the -i
switch means case insensitive in this case.
In that case, grep will show without color and without line numbers.
You can change that with the --color
and the -n
switches (Color and lines numbers in files respectively).
In the end, you can have something like :
find . -name '*.log' -type f -exec grep --color -n 'pattern' {} '+'
for instance
$ find . -name '*.c' -type f -exec grep -n 'hello' {} '+'
./test2/target.c:1:hello