删除文件中包含字符串的文件 - Linux cli
我试图通过 Linux CLI 在文件中查找电子邮件地址来删除错误的电子邮件。
我可以使用 find 获取文件
。 | xargs grep -l <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6c3cbc7cfcae6c3dec7cbd6cac388c5c9cb">[email protected]
但我不知道了解如何从那里删除它们,因为以下代码不起作用。
rm -f | rm -f | rm -f | rm -f | rm -f | rm -f | xargs 找到 . | xargs grep -l <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fa9f979b9396ba9f829b978a969fd4999597">[电子邮件受保护]
I am trying to delete erroneous emails based on finding the email address in the file via Linux CLI.
I can get the files with
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您的命令的解决方案:
或者
Solution for your command:
Or
为了安全起见,我通常将 find 的输出传输到 awk 之类的东西,并创建一个批处理文件,每行都是“rm 文件名”,
这样您就可以在实际运行它之前检查它,并手动修复任何难以用 awk 来完成的奇怪边缘情况。正则表达式
For safety I normally pipe the output from find to something like awk and create a batch file with each line being "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
的-exec
和-delete
,它只会删除文件,如果grep
命令成功了。使用grep -q
这样它就不会打印任何内容,您可以将-q
替换为-l
来查看哪些文件包含该字符串他们。You can use
find
's-exec
and-delete
, it will only delete the file if thegrep
command succeeds. Usinggrep -q
so it wouldn't print anything, you can replace the-q
with-l
to see which files had the string in them.我喜欢 Martin Beckett 的解决方案,但发现带有空格的文件名可能会出错(就像谁在文件名中使用空格一样,pfft :D)。另外,我想查看匹配的内容,因此我将匹配的文件移动到本地文件夹,而不是仅使用“rm”命令删除它们:
注意:我遇到了 grep 无法匹配具有 utf-16 编码的文件内部的问题。
请参阅此处了解解决方法。如果该网站消失,您要做的就是使用 grep 的 -a 标志,该标志使 grep 将文件视为文本并使用与每个扩展字符中的任何第一个字节匹配的正则表达式模式。例如,要匹配 Entité,请执行以下操作:
如果这不起作用,请尝试以下操作:
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:
NOTE: I had issues where grep could not match inside files that had utf-16 encoding.
See here for 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:
and if that doesn't work then try this:
尽管马丁的答案很安全,但如果您确定要删除的内容,例如在编写脚本时,我使用了 this 比这里之前建议的任何其他单行都取得了更大的成功:
但我宁愿通过名称查找:
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 this with greater success than any other one-liner suggested before around here:
But I rather find by name:
把工作做得更好。使用 `...` 运行命令以提供包含
[email] 的文件名;protected]
(grep -l
列出它们,-i
忽略大小写)以使用rm
删除它们(-f
强制/-i
交互)。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 withrm
(-f
forcibly /-i
interactively).如何删除:
how to remove:
快速高效。将
find_files_having_this_text
替换为您要搜索的文本。Quick and efficent. Replace
find_files_having_this_text
with the text you want to search.