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


当前回答

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

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

其他回答

我的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 ">"

下面的命令不需要find通过使用第二个grep来过滤svn文件夹。

grep -rL "foo" ./* | grep -v "\.svn"

下面的命令可以帮助你过滤包含子字符串“foo”的行。

cat file | grep -v "foo"
grep -irnw "filepath" -ve "pattern"

or

grep -ve "pattern" < file

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

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

$ grep -L "foo" *