[英]Removing files with rm using find and xargs
When I do当我做
rm file.txt
or或者
rm *.txt
I'm prompted for each file, since I didn't specify the -f
option to rm
.我被提示输入每个文件,因为我没有为rm
指定-f
选项。
But when I do this:但是当我这样做时:
find . -type f -name '*.txt' | xargs rm
the files are removed without the confirmation.未经确认删除文件。
What is the logics behind this?这背后的逻辑是什么? Is it possible to find the reason in some documentation?是否可以在某些文档中找到原因? I cannot explain why this would be the case.我无法解释为什么会这样。
You have an alias set for the rm command to 'rm -i'.您将 rm 命令的别名设置为“rm -i”。 Therefore if you invoke the command directly as in因此,如果您直接调用命令
rm file.txt
or或者
rm *.txt
the alias will be expanded.别名将被扩展。 If you will call it with xargs as in如果您将使用 xargs 调用它,如
find . -type f -name '*.txt' | xargs rm
The rm is passed as a simple string argument to xargs and is later invoked by xargs without alias substitution of the shell. rm 作为一个简单的字符串参数传递给 xargs,稍后由 xargs 调用,而无需对 shell 进行别名替换。 You alias is probably defined in ~/.bashrc, in case you want to remove it.您的别名可能在 ~/.bashrc 中定义,以防您想删除它。
根据您的 xargs 版本,您可能可以使用 --no-run-if-empty GNU 扩展选项:
find . -type f -name '*.txt' | xargs --no-run-if-empty rm -rf
你可以使用这个简单的命令来解决你的问题
find . --type f -name '*.txt' -delete
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.