linux - 使用 find 和 xargs 使用 rm 删除文件

标签 linux xargs rm

当我这样做

rm file.txt

rm *.txt

每个文件都会提示我,因为我没有为 rm 指定 -f 选项。

但是当我这样做的时候:

find . -type f -name '*.txt' | xargs rm

文件在未经确认的情况下被删除。

这背后的逻辑是什么?是否可以在某些文档中找到原因?我无法解释为什么会这样。

最佳答案

您将 rm 命令的别名设置为“rm -i”。因此,如果您像在

中那样直接调用命令
rm file.txt

rm *.txt

别名将被扩展。如果你像在

中那样用xargs调用它
find . -type f -name '*.txt' | xargs rm

rm 作为简单的字符串参数传递给 xargs,稍后由 xargs 调用,无需 shell 的别名替换。 你的别名可能在 ~/.bashrc 中定义,以防你想删除它。

关于linux - 使用 find 和 xargs 使用 rm 删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42828021/

相关文章:

linux - 递归地重命名文件并删除一个字符

git - 如何撤消 git rm 。 -r --cached 命令而不丢失任何未提交的更改?

linux - ESX 主机/硬件是什么意思

linux - 无法在 AWS EC2 AMI 中安装 GConf2

linux - 带握手的 g_serial 小工具

Bash: "xargs cat",在每个文件后添加换行符

linux - xargs -I %/路径/%

linux - 删除名为 ~ 的文件

zsh - 在 zsh 中确认 rm

linux - 打开新的 shell session 时,我的 Bash 脚本不响应 SIGNALS