使用查找删除早于n天的目录

时间:2013-11-09 04:59:57

标签: shell unix

我的要求与此问题几乎相同:Shell script to delete directories older than n days 我的目录看起来像这样:

Jul 24 05:46 2013_07_24

Jul 31 22:30 2013_08_01

Sep 18 05:43 2013_09_18

Oct 07 08:41 2013_10_07

我想删除超过90天的任何内容。基于上述线程中给出的解决方案,我在我的脚本中使用了以下内容:

find $BASE_DIR  -type d -ctime +90 -exec rm -rf  {} \;

脚本已成功删除目录,但它也因此错误而失败:

find: 0652-081 cannot change directory to <actual_path>:
  : A file or directory in the path name does not exist.

这里唯一的事情是$ BASE_DIR指向虚拟位置的位置,错误消息中的actual_path指向实际位置。环境中有软链接。

1 个答案:

答案 0 :(得分:1)

尝试

find $BASE_DIR -mindepth 1 -maxdepth 1 -type d -ctime +90 -exec rm -rf  {} \;

这只会覆盖$ BASE_DIR下的目录,但应避免生成该错误消息。