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

删除在file-linux cli中找到字符串的文件,linux,file,find,command-line-interface,rm,Linux,File,Find,Command Line Interface,Rm,我试图通过Linux CLI在文件中查找电子邮件地址来删除错误的电子邮件 我可以用它来获取文件 find.|xargs grep-lemail@domain.com 但是我不知道如何从那里删除它们,因为下面的代码不起作用 rm-f | xargs find.|xargs grep-lemail@domain.com 谢谢您的帮助。为了安全起见,我通常将find的输出通过管道传输到类似awk的东西,并创建一个批处理文件,每行都是“rm filename” 这样,您可以在实际运行它之前检查它,并手动

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

我可以用它来获取文件

find.|xargs grep-lemail@domain.com

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

rm-f | xargs find.|xargs grep-lemail@domain.com


谢谢您的帮助。

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

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

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

@马丁·贝克特(Martin Beckett)给出了一个很好的答案,请遵循该指南

针对您的命令的解决方案:

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


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

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

尽管Martin给出了安全的答案,但如果您确定了要删除的内容,例如在编写脚本时,我使用了比之前任何一行建议都更成功的方法:

$ find . | grep -l email@domain.com | xargs -I {} rm -rf {}
但我更喜欢通过名字发现:

$ find . -iname *something* | xargs -I {} echo {}
如何删除:

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

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

我喜欢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 -a 'E.n.t.i.t.e'

查找-执行官grep-q't-bone@spechal.com' '{}' \; -打印
显示任何内容?是的,但没有显示预期内容<代码>查找|格雷普-bone@spechal.com“另一方面,它工作得很好。顺便说一句,我在mac电脑上。我的答案为我解决了这个问题我喜欢你的方法,但对我来说,我不能这样做,因为我需要一个cron作业:P所以我要用这个,我如何计算删除文件的数量?通过管道将命令发送到
wc-l
似乎不起作用。如何执行
查找-键入f
?欢迎使用堆栈溢出!虽然这个代码片段可以解决这个问题,但它确实有助于提高文章的质量。请记住,您将在将来回答读者的问题,而这些人可能不知道您的代码建议的原因。-对于多个文件,可以使用
grep-l-R--include=“*”防止通配符添加过多参数email@domain.com./
而不是
sudo grep-lr'/directory/youd/like/to/delete/from/'-e'您要搜索的文本'| xargs rm
这是我使用的。我相信2grit引用了'-r'表示递归,这在我的例子中很有用。
rm -f `find . | xargs grep -li email@domain.com`
# 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 -a 'Entit.e'
grep -a 'E.n.t.i.t.e'