【问题标题】:Delete files with string found in file - linux cli删除文件中包含字符串的文件 - linux cli
【发布时间】:2011-05-30 13:43:11
【问题描述】:

我正在尝试根据通过 Linux CLI 查找文件中的电子邮件地址来删除错误的电子邮件。

我可以通过

获取文件

find . | xargs grep -l email@domain.com

但我不知道如何从那里删除它们,因为以下代码不起作用。

rm -f | xargs find . | xargs grep -l email@domain.com

感谢您的帮助。

【问题讨论】:

    标签: linux file find command-line-interface rm


    【解决方案1】:

    我喜欢 Martin Beckett 的解决方案,但发现带有空格的文件名可能会出错(例如谁在文件名中使用空格,pfft :D)。我还想查看匹配的内容,所以我将匹配的文件移动到本地文件夹,而不是使用“rm”命令删除它们:

    # Make a folder in the current directory to put the matched files
    $ mkdir -p './matched-files'
    
    # Create a script to move files that match the grep
    # NOTE: Remove "-name '*.txt'" to allow all file extensions to be searched.
    # NOTE: Edit the grep argument 'something' to what you want to search for.
    
    $ find . -name '*.txt' -print0 | xargs -0 grep -al 'something' | awk -F '\n' '{ print "mv \""$0"\" ./matched-files" }' > doit.sh
    
    Or because its possible (in Linux, idk about other OS's) to have newlines in a file name you can use this longer, untested if works better (who puts newlines in filenames? pfft :D), version:
    
    $ find . -name '*.txt' -print0 | xargs -0 grep -alZ 'something' | awk -F '\0' '{ for (x=1; x<NF; x++) print "mv \""$x"\" ./matched-files" }' > doit.sh
    
    # Evaluate the file following the 'source' command as a list of commands executed in the current context:
    $ source doit.sh
    

    注意:我遇到了 grep 无法匹配具有 utf-16 编码的文件的问题。 有关解决方法,请参阅 here。如果该网站消失,您所做的是使用 grep 的 -a 标志,该标志使 grep 将文件视为文本并使用匹配每个扩展字符中的任何第一个字节的正则表达式模式。例如要匹配 Entité,请执行以下操作:

    grep -a 'Entit.e'
    

    如果这不起作用,那么试试这个:

    grep -a 'E.n.t.i.t.e'
    

    【讨论】:

      【解决方案2】:
      rm -f `find . | xargs grep -li email@domain.com`
      

      做得更好。使用 `...` 运行命令以提供包含 email.@domain.com 的文件名(grep -l 列出它们,-i 忽略大小写)以使用 rm 删除它们(-f 强制 / -i 交互)。

      【讨论】:

        【解决方案3】:
        find . | xargs grep -l email@domain.com
        

        如何删除:

        rm -f 'find . | xargs grep -l email@domain.com'
        

        【讨论】:

        • 欢迎来到 Stack Overflow!虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。 - From review
        【解决方案4】:

        @Martin Beckett 发布了一个很好的答案,请遵循该指南

        您的命令的解决方案:

        grep -l email@domain.com * | xargs rm
        

        或者

        for file in $(grep -l email@domain.com *); do
            rm -i $file;
            #  ^ prompt for delete
        done
        

        【讨论】:

        • 对于多个文件,可以使用grep -l -R --include="*" email@domain.com ./来防止通配符添加过多参数
        • sudo grep -lr '/directory/youd/like/to/delete/from/' -e 'text you would like to search' | xargs rm 这是我用的。我相信 2grit 引用了递归的“-r”,这对我的情况很有帮助。
        【解决方案5】:

        你可以使用find-exec-delete,只有grep命令成功才会删除文件。使用grep -q 使其不会打印任何内容,您可以将-q 替换为-l 以查看哪些文件中包含该字符串。

        find . -exec grep -q 'email@domain.com' '{}' \; -delete
        

        【讨论】:

        • find . -exec grep -q 't-bone@spechal.com' '{}' \; -print 显示什么?
        • 是的,但没有像预期的那样。 find . |grep 't-bone@spechal.com' 另一方面工作得很好。我在Mac上,顺便说一句。无论如何,我的回答为我解决了。 ;)
        【解决方案6】:

        尽管 Martin 给出了安全的答案,但如果您确定要删除的内容(例如在编写脚本时),我使用 this 比之前建议的任何其他单行代码更成功:

        $ find . | grep -l email@domain.com | xargs -I {} rm -rf {}
        

        但我宁愿按名称查找:

        $ find . -iname *something* | xargs -I {} echo {}
        

        【讨论】:

          【解决方案7】:

          为了安全起见,我通常将 find 的输出通过管道传输到 awk 之类的内容,并创建一个批处理文件,其中每一行都是“rm 文件名”

          这样你就可以在实际运行它之前检查它并手动修复任何用正则表达式难以处理的奇怪边缘情况

          find . | xargs grep -l email@domain.com | awk '{print "rm "$1}' > doit.sh
          vi doit.sh // check for murphy and his law
          source doit.sh
          

          【讨论】:

          • 我喜欢你的方法,但对我来说不能这样做,因为我需要一个 cron 工作:P 所以我会用这个 stackoverflow.com/a/4529188/656094
          • 如何统计已删除文件的数量?将命令传送到wc -l 似乎不起作用。
          • find . -type f 怎么样?
          猜你喜欢
          • 1970-01-01
          • 2012-07-02
          • 1970-01-01
          • 2014-04-24
          • 2021-03-17
          • 2021-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多