views:

16

answers:

2

I need to find ALL files that have multiple keywords anywhere in the file (not necessarily on the same line), given a starting directory like ~/. Does "grep -ro" do this?

(I'm using Unix, Mac OSX 10.4)

A: 

You can use the -l option to get a list of filenames with matches, so it's just a matter of finding all of the files that have the first keyword and then filtering that list down to the files that also have the second keyword:

grep -rl first_keyword basedir | xargs grep -l second_keyword
Adam Rosenfield
A: 

To search just *.txt

find ~/. -name "*.txt" | xargs grep -l first_keyword | xargs grep -l second_keyword

Thanks Adam!

russian_spy
note that "find" doesn't work for paths containing spaces like "my path/"
russian_spy
(or for filenames containing spaces like "my file name.txt")
russian_spy