如何在Linux中查找包含两个字符串的文件?

alw*_*btc 12 linux

我想找到包含两个字符串的文件,例如文件同时包含string1string2

我想要输出中文件的完整路径。我不想看到“权限被拒绝”警告。

Red*_*ick 18

grep -l string2 `grep -l string1 /path/*`
Run Code Online (Sandbox Code Playgroud)

这与

grep -l string2 $(grep -l string1 /path/*)
Run Code Online (Sandbox Code Playgroud)

编辑:这就是为什么grep string1 /path/* | grep string2不做我认为 alwbtc 想要的。

$ cd /tmp
$ cat a
apples
oranges
bananas
$ cat b
apples
mangoes
lemons
$ cat c
mangoes
limes
pears
$ cd ~
$ grep apples /tmp/* | grep mangoes
$
Run Code Online (Sandbox Code Playgroud)

未找到任何内容,但文件 b 包含两个字符串。

这就是我认为 alwbtc 想要的

$ grep -l apples $(grep -l mangoes /tmp/*)
/tmp/b
Run Code Online (Sandbox Code Playgroud)