我正在尝试运行以下命令来删除超过 3 分钟的 tmp 文件夹:
find /tmp/tmp* -mmin +3 -exec rm -rf {} \;
上述命令失败。为什么?
find /tmp/tmp* -mmin +3 -exec rm -rf {} \;
find: `/tmp/tmpAJaHLX/crashes': No such file or directory
find: `/tmp/tmpbM5ac8/minidumps': No such file or directory
find: `/tmp/tmpbM5ac8/crashes/events': No such file or directory
find: `/tmp/tmpdQACdf/gmp': No such file or directory
find: `/tmp/tmpo1tbqu/thumbnails': No such file or directory
find: `/tmp/tmpW91Yel/extensions': No such file or directory
答案1
find
您所看到的内容可能是该命令的结果遍历顺序。
例如给定一个简单的目录
$ find somedir
somedir
somedir/file
然后
$ find somedir -exec rm -rfv {} \;
removed ‘somedir/file’
removed directory: ‘somedir’
find: `somedir': No such file or directory
您可以使用选项强制深度优先遍历-depth
,即
$ find somedir -depth -exec rm -rfv {} \;
removed ‘somedir/file’
removed directory: ‘somedir’
不会导致错误。
或者,您可以使用更简单的find somedir -delete
方法(如手册页中所述)来打开-depth
选项
-delete
Delete files; true if removal succeeded. If the removal failed,
an error message is issued. If -delete fails, find's exit sta‐
tus will be nonzero (when it eventually exits). Use of -delete
automatically turns on the -depth option.