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


当前回答

我的grep没有任何-L选项。我确实找到了解决办法。

这些想法是:

将包含相应字符串的所有文件名转储到txt1.txt。 将目录中的所有文件名转储到txt2.txt文件中。 使用diff命令区分2个转储文件。 Grep 'foo' *.log | cut -c1-14 | uniq > txt1.txt Grep * *.log | cut -c1-14 | uniq > txt2.txt Diff txt1.txt txt2.txt | 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

你实际上需要:

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 -H -E -o -c "foo" */*/*.ext | grep ext:0

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