删除文件中包含字符串的文件 - Linux cli

发布于 2024-10-09 02:26:01 字数 497 浏览 4 评论 0原文

我试图通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

忆依然 2024-10-16 02:26:01

您的命令的解决方案:

grep -l [email protected] * | xargs rm

或者

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

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
蓦然回首 2024-10-16 02:26:01

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

这样您就可以在实际运行它之前检查它,并手动修复任何难以用 awk 来完成的奇怪边缘情况。正则表达式

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

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 . | xargs grep -l [email protected] | awk '{print "rm "$1}' > doit.sh
vi doit.sh // check for murphy and his law
source doit.sh
眼藏柔 2024-10-16 02:26:01

您可以使用find-exec-delete,它只会删除文件,如果grep命令成功了。使用 grep -q 这样它就不会打印任何内容,您可以将 -q 替换为 -l 来查看哪些文件包含该字符串他们。

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

You can use find's -exec and -delete, it will only delete the file if the grep command succeeds. Using grep -q so it wouldn't print anything, you can replace the -q with -l to see which files had the string in them.

find . -exec grep -q '[email protected]' '{}' \; -delete
梦罢 2024-10-16 02:26:01

我喜欢 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 编码的文件内部的问题。
请参阅此处了解解决方法。如果该网站消失,您要做的就是使用 grep 的 -a 标志,该标志使 grep 将文件视为文本并使用与每个扩展字符中的任何第一个字节匹配的正则表达式模式。例如,要匹配 Entité,请执行以下操作:

grep -a 'Entit.e'

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

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

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:

# 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

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:

grep -a 'Entit.e'

and if that doesn't work then try this:

grep -a 'E.n.t.i.t.e'
想你只要分分秒秒 2024-10-16 02:26:01

尽管马丁的答案很安全,但如果您确定要删除的内容,例如在编写脚本时,我使用了 this 比这里之前建议的任何其他单行都取得了更大的成功:

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

但我宁愿通过名称查找:

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

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:

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

But I rather find by name:

$ find . -iname *something* | xargs -I {} echo {}
痕至 2024-10-16 02:26:01
rm -f `find . | xargs grep -li [email protected]`

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

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).

删除会话 2024-10-16 02:26:01
find . | xargs grep -l [email protected]

如何删除:

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

how to remove:

rm -f 'find . | xargs grep -l [email protected]'
独木成林 2024-10-16 02:26:01

快速高效。将 find_files_having_this_text 替换为您要搜索的文本。

grep -Ril 'find_files_having_this_text'  . |  xargs rm

Quick and efficent. Replace find_files_having_this_text with the text you want to search.

grep -Ril 'find_files_having_this_text'  . |  xargs rm
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文