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


当前回答

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

git grep -L "foo"

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

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

其他回答

如果你的grep有-L(或——files-without-match)选项:

$ grep -L "foo" *

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

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

你实际上需要:

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

为了完整,ripgrep版本如下:

rg --files-without-match "pattern"

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

rg --files-without-match -t ruby "frozen_string_literal: true" app/
grep -irnw "filepath" -ve "pattern"

or

grep -ve "pattern" < file

上面的命令会给我们结果,因为-v找到了正在搜索的模式的倒数