查找不包含字符串的文件 [英] Find files that does not contain a string
问题描述
要查找当前文件夹中包含foo的所有文件,我使用:
To find all the files that contain "foo" in current folder, I use:
grep -r "foo" .
要查找当前文件夹中包含bar的所有文件,我使用:
To find all the files that contain "bar" in current folder, I use:
grep -r "bar" .
但是如何找到所有不包含'foo'和'bar'的文件?
But how to find all files that does not contain 'foo' and 'bar'?
推荐答案
要打印不包含某些字符串的行,可以使用 -v
标记:
To print lines that do not contain some string, you use the -v
flag:
grep -r -v "bar" . | grep -v "foo"
这会为您提供不包含 foo
或 bar
。
打印文件不包含某些字符串,您可以使用 -L
标志。为了不匹配多个字符串,可以使用带有 -P
标志的正则表达式(可以使用多个正则表达式标志):
To print files that do not contain some string, you use the -L
flag. To non-match several strings, you can use regular expressions with the -P
flag (there are several regex flags you can use):
grep -r -L -P "(foo|bar)" .
这会打印一个不包含 foo
或 bar
。
This prints a list of files that don't contain foo
or bar
.
感谢 Anton Kovalenko 指出了这一点。
这篇关于查找不包含字符串的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!