linux - 删除所有超过 60 天的目录及其文件,无论目录是否为空

标签 linux bash shell ubuntu

这个问题在这里已经有了答案:





find command in bash script resulting in "No such file or directory" error only for directories?

(2 个回答)


1年前关闭。




我正在尝试删除 /mnt/games/codes 中的所有目录早于 60 天。目录可以为空,也可以不为空,但我希望将它们全部删除。
所以看这里,我发现了这个命令:

find /mnt/games/codes/* -mtime +60 -type d -exec rm -rf {} \;
但它给了我这个错误:

No such file or directory


所以我尝试了这个:
find /mnt/games/codes/* -mtime +60 -type d -exec rmdir {} \;
但它给了我一个看起来像这样的全新错误:

Directory not empty


有没有办法用一个命令删除这些?
谢谢!

最佳答案

您的问题是因为 find在完成迭代之前删除它的搜索基础目录,所以调用你的rm -rf在已删除的条目上。
这很容易通过添加 -depth 来解决。选项。
另外,你真的应该结束rm带有双破折号的选项 -- , 以防止 find 提供参数命令,被 rm 解释为选项参数命令。

find /mnt/games/codes/ -depth -mtime '+60' -type d -exec rm -rf -- {} \;

关于linux - 删除所有超过 60 天的目录及其文件,无论目录是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64539725/

相关文章:

linux - 从 shellcode 启动的 Shell 在启动时立即停止

java - ADB shell 获取 Toast 消息

shell - 如何从 Artifactory 存储库下载最新的 Artifactory ?

c++ -/usr/include/boost/filesystem/path.hpp:307: 未定义对`boost::filesystem::path::operator 的引用

linux - 在 Linux 中通过蓝牙访问 Neurosky Mindset 的串行数据

c - snprintf 手册页示例内存泄漏?

php - 我如何在php:5.4-apache docker中安装zip扩展

linux - 出现提示时 Bash 脚本类型输入

linux - 如何在bash函数中保留引号?

windows - 使用 R 中的 shell 或管道读取 csv 文件的列 - Windows