Wednesday, September 28, 2005

Searching directories and subdirectories for a text string

This seems like a basic operation, yet I could not find documentation on it. It's plagued me for years, and I'm proud to tell you, here is the solution:

find path -name "file pattern" -print -exec grep "Regular Expression" {} \;

Note the space between {} and \; if you don't have it, you'll get an "Incomplete Statement" error. Always great when whitespace has meaning...

Tack on &> filename to output to a file. Here's an example:

find . -name "*.doc" -print -exec grep "secrets" {} \; &> FINDOUTPUT.txt

1 comment:

Anonymous said...

... or you could say:
egrep -r "Regular Expression" --include="file pattern" path

although this does not print names of files not containing matches.

Labels

Blog Archive

Contributors