如何找出当前目录中不包含单词foo(使用grep)的文件?
当前回答
您可以单独使用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
为了完整,ripgrep版本如下:
rg --files-without-match "pattern"
你可以结合文件类型和搜索路径,例如:
rg --files-without-match -t ruby "frozen_string_literal: true" app/
如果你的grep有-L(或——files-without-match)选项:
$ grep -L "foo" *
grep -irnw "filepath" -ve "pattern"
or
grep -ve "pattern" < file
上面的命令会给我们结果,因为-v找到了正在搜索的模式的倒数
下面的命令给出了所有不包含foo模式的文件:
find . -not -ipath '.*svn*' -exec grep -H -E -o -c "foo" {} \; | grep 0