gpt4 book ai didi

shell - 为什么 shell 命令找不到 . -名称 "*.txt"| rm 删除当前目录中的所有.txt 文件?

转载 作者:行者123 更新时间:2023-12-02 09:36:39 25 4
gpt4 key购买 nike

我是 shell 新手。我尝试使用以下命令删除当前目录中的所有.txt文件。

find . -name "*.txt" | rm

我意识到这并不是删除所有 .txt 文件的最直接的方法——这只是一个管道实验。但它只是返回 rm 的使用信息...并没有达到我的预期。

我的误解在哪里?谢谢!

最佳答案

您应该使用xargs将管道输入的数据视为参数:

find . -name "*.txt" | xargs rm

当然,还有更简单的方法可以做到这一点:

find . -name '*.txt' -exec rm {} \;

rm *.txt

请注意,后者仅删除当前目录中的文件,而使用 find 会进行递归搜索。

关于shell - 为什么 shell 命令找不到 . -名称 "*.txt"| rm 删除当前目录中的所有.txt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25462112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com