Grep for multiple patterns across multiple files

I walked around the world and I can not find the answer I am looking for.

Let's say I have a file text1.txtin a directory mydirwhose contents are:

one
two

and the other text2.txt, also in mydir, the contents of which:

two
three
four

I am trying to get a list of files (for a given directory) that contain all (not any) of the patterns I am looking for. In the example I cited, I am looking for a way out somewhere along the lines:

./text1.txt

or

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

The only thing I managed to find was matching any patterns in a file or matching several patterns in a single file (which I tried to distribute to the entire directory, but got errors using grep).

Any help is greatly appreciated.

Edit-Things I tried

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

ambiguous redirection

grep 'pattern1'|'pattern2' ./*

,

+5
3

:

find . | xargs grep 'pattern1' -sl | xargs grep 'pattern2' -sl
+8

, , ( )

grep -EH 'pattern1|pattern2' mydir
+3

Improve the brain response:

find . -type f -print0 | xargs -0 grep 'pattern1' -slZ | xargs -0 grep 'pattern2' -sl

This will make grep try to look for directories and can correctly handle file names with spaces if you pass the -Z flag to grep for all but the last pattern and pass -0 to xargs.

+2
source

All Articles