grep
Robert Herrage
herrage at ntpal.UUCP
Thu Oct 25 22:52:32 AEST 1990
In article <1990Oct23.123025.18012 at kodak.kodak.com>, tiefel at sunshine.Kodak.COM (Lenny Tiefel) writes:
> I have a main directory with hundreds of subdirectories,
> and I want to find a file with a particular string, say "xyz"
> The grep command only works in one directory at a time. Is there
> a way of searching my whole directory structure to find a file
> with a particular string?
Here's a nice implementation, appropriately named "rgrep" (Recursive GREP):
#!/bin/sh
{ find . \( -name '*.*' \) -exec grep -l $* {} \; -exec grep -n $* \; -exec echo \; | more; }
By getting into the top-most directory and typing
rgrep xyz
you would get something like this:
./subdir1/file1
136: this line has xyz in it
210: this line also has xyz in it
./subdir2/subsubdir4/file2
12: this line has xyz in it
I believe the "grep -l" causes the "./subdir/file1" to be printed and the
"grep -n" causes the line numbers to be printed. The "echo", of course,
gives you a blank line separation in case the string exists in more than
one file.
If you want to limit your searches to specific file extensions, you could
replace the "\( -name '*.*' \)" with something like
\( -name '*.[chCH]' -o -name '*.ec' -o -name '*.txt' \)
which means only files with a ".c", ".h", ".C", ".H", ".ec", or ".txt"
extension will be searched.
Enjoy!
Robert
(Thanks Dana Cavasso, author!)
More information about the Comp.unix.questions
mailing list