Linux – How to find files containing two strings together in Linux

linux

I want to find files containing two strings together, for example the file contains both string1 and string2.

I want the full path of files in output. I don't want to see "permission denied" warnings.

Best Answer

grep -l string2 `grep -l string1 /path/*`

which is the same as

grep -l string2 $(grep -l string1 /path/*)

Edit: heres why grep string1 /path/* | grep string2 doesn't do what I think alwbtc wants.

$ cd /tmp
$ cat a
apples
oranges
bananas
$ cat b
apples
mangoes
lemons
$ cat c
mangoes
limes
pears
$ cd ~
$ grep apples /tmp/* | grep mangoes
$

Nothing found, but file b contains both strings.

Here's what I think alwbtc wants

$ grep -l apples $(grep -l mangoes /tmp/*)
/tmp/b
Related Question