【问题标题】:How to find text files not containing text on Linux?如何在 Linux 上查找不包含文本的文本文件?
【发布时间】:2011-11-04 12:32:08
【问题描述】:

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

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

【问题讨论】:

标签: linux find text-processing


【解决方案1】:

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

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

只说一个。


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

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

是的倒数

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

顺便考虑一下

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

甚至

grep -IRiL "somestring" .

【讨论】:

  • 接受你的答案,它是我想要的 find 的递归和逆: grep -IRiL "somestring" 。
【解决方案2】:

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

grep -riL "somestring" .

这是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

【讨论】:

    【解决方案3】:

    通过find和grep查找markdown文件找到不匹配的地方

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

    直接使用grep的-L搜索只包含markdown文件而没有title的文件

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

    【讨论】:

      【解决方案4】:

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

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

      直接使用“grep”:

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

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

      【讨论】:

        猜你喜欢
        • 2015-10-07
        • 2017-07-03
        • 2013-06-02
        • 2017-01-02
        • 2012-05-22
        • 2018-05-06
        • 2023-03-30
        • 2011-09-03
        相关资源
        最近更新 更多