如何找出当前目录中不包含单词foo(使用grep)的文件?


当前回答

来看看ack。它自动为您排除.svn,为您提供Perl正则表达式,并且是一个简单的Perl程序下载。

在ack中,与你要找的内容等价:

ack -L foo

其他回答

你实际上需要:

find .  -not  -ipath '.*svn*' -exec  grep  -H -E -o -c  "foo"  {} \; | grep :0\$

我运气很好

grep -H -E -o -c "foo" */*/*.ext | grep ext:0

我用grep -v的尝试只是给了我所有没有“foo”的行。

当grep没有-L选项时(例如IBM AIX),只有grep和shell时的另一种选择:

for file in * ; do grep -q 'my_pattern' $file || echo $file ; done

为了完整,ripgrep版本如下:

rg --files-without-match "pattern"

你可以结合文件类型和搜索路径,例如:

rg --files-without-match -t ruby "frozen_string_literal: true" app/

下面的命令给出了所有不包含foo模式的文件:

find .  -not  -ipath '.*svn*' -exec  grep  -H -E -o -c  "foo"  {} \; | grep 0