如何在 Linux 上查找不包含文本的文本文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7205004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:53:47  来源:igfitidea点击:

How to find text files not containing text on Linux?

linuxfindtext-processing

提问by eon

How do I find files notcontaining some text on Linux? Basically I'm looking for the inverse of the following

如何在 Linux 上查找包含某些文本的文件?基本上我正在寻找以下内容的倒数

find . -print | xargs grep -iL "somestring"

采纳答案by sehe

The command you quote, ironically enough does exactly what you describe. Test it!

具有讽刺意味的是,您引用的命令完全符合您的描述。测试一下!

echo "hello" > a
echo "bye" > b
grep -iL BYE a b

Says a only.

只说一个。



I think you may be confusing -L and -l

我想你可能会混淆 -L 和 -l

find . -print | xargs grep -iL "somestring"

isthe inverse of

相反的

find . -print | xargs grep -il "somestring"

By the way, consider

顺便考虑一下

find . -print0 | xargs -0 grep -iL "somestring"

Or even

甚至

grep -IRiL "somestring" .

回答by danilo

If you use "find" the script do "grep" also in folder:

如果您使用“查找”脚本也在文件夹中执行“grep”:

[root@vps test]# find  | xargs grep -Li 1234
grep: .: Is a directory
.
./test.txt
./test2.txt
[root@vps test]#

Use the "grep" directly:

直接使用“grep”:

# grep -Li 1234 /root/test/*
/root/test/test2.txt
/root/test/test.txt
[root@vps test]#

or specify in "find" the options "-type f"...even if you use the find you will put more time (first the list of files and then make the grep).

或在“查找”中指定选项“-type f”...即使您使用查找,您也会投入更多时间(首先是文件列表,然后是 grep)。

回答by Adrian

You can do it with grep alone (without find).

您可以单独使用 grep 来完成(无需查找)。

grep -riL "somestring" .

This is the explanation of the parameters used on grep

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

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

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

If you use llowercase you will get the opposite (files with matches)

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

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

回答by lupguo

Find the markdown file through find and grep to find the mismatch

通过find和grep查找markdown文件查找不匹配

$ find. -name '* .md' -print0 | xargs -0 grep -iL "title"

Directly use grep's -Lto search for files that only contain markdown files and no titles

直接使用grep's-L搜索只包含markdown文件而没有标题的文件

$ grep -iL "title" -r ./* --include '* .md'