这个问题在这里已经有了答案:
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/