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


当前回答

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

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

ack -L foo

其他回答

你实际上需要:

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

如果你正在使用git,它会搜索所有被跟踪的文件:

git grep -L "foo"

如果你打开了**子目录globbing,你可以在跟踪文件的子集中搜索(在.bashrc中使用shopt -s globstar):

git grep -L "foo" -- **/*.cpp

这可能会对其他人有所帮助。我有混合文件Go和测试文件。但我只需要。go文件。所以我用了

ls *.go | grep -v "_test.go"

-v,——invert-match选择不匹配的行参见https://stackoverflow.com/a/3548465

也可以使用vscode从终端打开所有文件

code $(ls *.go | grep -v "_test.go")

您可以单独使用grep(没有find)来完成。

grep -riL "foo" .

这是对grep上使用的参数的解释

     -L, --files-without-match
             each file processed.
     -R, -r, --recursive
             Recursively search subdirectories listed.

     -i, --ignore-case
             Perform case insensitive matching.

如果你使用l(小写),你将得到相反的(文件匹配)

     -l, --files-with-matches
             Only the names of files containing selected lines are written

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

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