查找不包含字符串的文件

nam*_*nam 9 regex linux grep

要查找当前文件夹中包含"foo"的所有文件,我使用:

grep -r "foo" .
Run Code Online (Sandbox Code Playgroud)

要查找当前文件夹中包含"bar"的所有文件,我使用:

grep -r "bar" .
Run Code Online (Sandbox Code Playgroud)

但是如何查找不包含'foo'和'bar'的所有文件?

ale*_*nis 18

要打印线不包含某些字符串,可以使用-v标志:

grep -r -v "bar" . | grep -v "foo"
Run Code Online (Sandbox Code Playgroud)

这将为您提供所有包含foo或的行bar.

要打印不包含某些字符串的文件,请使用该-L标志.要不匹配多个字符串,可以使用带有-P标志的正则表达式(可以使用几个正则表达式标志):

grep -r -L -P "(foo|bar)" .
Run Code Online (Sandbox Code Playgroud)

这将打印一个不包含foo或的文件列表bar.

感谢Anton Kovalenko指出这一点.

  • 错误.`-v`是关于存在不匹配的行,而不是*匹配的行不存在*. (3认同)

小智 7

递归搜索目录中不包含 XYZ 的所有文件

find . -type f | xargs grep -L "XYZ"
Run Code Online (Sandbox Code Playgroud)