views:

33

answers:

2

How to find all files with a specific key word in a directory with subdirectories. For example, given a directory r_dir and subdirectoris d1, and d2, I need find all files contains "key_word" under r_dir and d1 and d2.

+1  A: 

On Windows:

findstr /spin /c:"key_word" *.*

(s = recursive, p = ignore binaries, i = case-insensitive, n = line numbers)

On linux / OS X:

grep -i -r key_word *

(i = case-insensitive, r = recurse subdirs)
jeffamaphone
coool! How to make the exact match? say, only "key_word" will be report but not "key_words".
Paul
If you want key_word on its own on a line, then search for "^key_word$". If you want an exact match within a line, I can't find a way to do it so far without resorting to further greps.
chrisbtoo
egrep -i -r "(\W|^)key_word($|\W)" *
chrisbtoo
There is no `-r` option in POSIX `grep`.
Jörg W Mittag
+1  A: 

I use something like:

find r_dir -type f -exec grep "key_word" {} \; -print
chrisbtoo
@chrisbtoo, yours seems faster than jeffamaphone...
Paul
There's a decent chance that's because mine is case-sensitive, whereas jeff's is case-insensitive. The character folding involved might well slow it down.
chrisbtoo