删除在文件中找到的带有字符串的文件 - linux cli

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4529134/
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-04 00:03:20  来源:igfitidea点击:

Delete files with string found in file - linux cli

linuxfilefindcommand-line-interfacerm

提问by Spechal

I am trying to delete erroneous emails based on finding the email address in the file via Linux CLI.

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

I can get the files with

我可以得到文件

find . | xargs grep -l [email protected]

find . | xargs grep -l [email protected]

But I cannot figure out how to delete them from there as the following code doesn't work.

但我无法弄清楚如何从那里删除它们,因为以下代码不起作用。

rm -f | xargs find . | xargs grep -l [email protected]

rm -f | xargs find . | xargs grep -l [email protected]

Thank you for your assistance.

谢谢您的帮助。

采纳答案by Martin Beckett

For safety I normally pipe the output from find to something like awk and create a batch file with each line being "rm filename"

为了安全起见,我通常将输出从 find 传送到 awk 之类的东西,并创建一个批处理文件,每一行都是“rm filename”

That way you can check it before actually running it and manually fix any odd edge cases that are difficult to do with a regex

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

find . | xargs grep -l [email protected] | awk '{print "rm "}' > doit.sh
vi doit.sh // check for murphy and his law
source doit.sh

回答by ajreal

@Martin Beckett posted an excellent answer, please follow that guideline

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

solution for your command :

您的命令的解决方案:

grep -l [email protected] * | xargs rm

Or

或者

for file in $(grep -l [email protected] *); do
    rm -i $file;
    #  ^ prompt for delete
done

回答by OneOfOne

You can use find's -execand -delete, it will only delete the file if the grepcommand succeeds. Using grep -qso it wouldn't print anything, you can replace the -qwith -lto see which files had the string in them.

您可以使用find's-exec-delete,它只会在grep命令成功时删除文件。使用grep -q它不会打印任何内容,您可以替换-qwith-l以查看哪些文件中包含该字符串。

find . -exec grep -q '[email protected]' '{}' \; -delete

回答by cregox

Despite Martin's safe answer, if you've got certainty of what you want to delete, such as in writing a script, I've used thiswith greater success than any other one-liner suggested before around here:

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

$ find . | grep -l [email protected] | xargs -I {} rm -rf {}

But I rather find by name:

但我宁愿按名称查找:

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

回答by marian

find . | xargs grep -l [email protected]

how to remove:

如何去除:

rm -f 'find . | xargs grep -l [email protected]'

回答by Kastor Stein

rm -f `find . | xargs grep -li [email protected]`

does the job better. Use `...` to run the command to offer the file names containing [email protected] (grep -l lists them, -i ignores case) to remove them with rm (-f forcibly / -i interactively).

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

回答by FocusedWolf

I liked Martin Beckett's solution but found that file names with spaces could trip it up (like who uses spaces in file names, pfft :D). Also I wanted to review what was matched so I move the matched files to a local folder instead of just deleting them with the 'rm' command:

我喜欢 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 \""
grep -a 'Entit.e'
"\" ./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 '
grep -a 'E.n.t.i.t.e'
' '{ 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


NOTE: I had issues where grep could not match inside files that had utf-16 encoding. See herefor a workaround. In case that website disappears what you do is use grep's -a flag which makes grep treat files as text and use a regex pattern that matches any first-byte in each extended character. For example to match Entité do this:

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

##代码##

and if that doesn't work then try this:

如果这不起作用,请尝试以下操作:

##代码##