Grep for multiple patterns over multiple files

Posted by prelic on Stack Overflow See other posts from Stack Overflow or by prelic
Published on 2011-09-14T20:43:39Z Indexed on 2012/03/19 10:04 UTC
Read the original article Hit count: 194

Filed under:

I've been googling around, and I can't find the answer I'm looking for.

Say I have a file, text1.txt, in directory mydir whose contents are:

one
two

and another called text2.txt, also in mydir, whose contents are:

two
three
four

I'm trying to get a list of files (for a given directory) which contain all (not any) patterns I search for. In the example I provided, I'm looking for output somewhere along the lines of:

./text1.txt

or

./text1.txt:one
./text1.txt:two

The only things I've been able to find are concerning matching any patterns in a file, or matching multiple patterns in a single file (which I tried extending to a whole directory, but received grep usage errors).

Any help is much appreciated.

Edit-Things I've tried

grep "pattern1" < ./* | grep "pattern2" ./*

"ambiguous redirect"

grep 'pattern1'|'pattern2' ./*

returns files that match either pattern

© Stack Overflow or respective owner

Related posts about grep