【问题标题】:Delete Directory Older Than n Days Using Find使用 Find 删除超过 n 天的目录
【发布时间】:2013-11-09 04:59:57
【问题描述】:

我的要求和这个问题差不多: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 指向实际位置。环境中有软链接。

【问题讨论】:

  • 你能发布find $BASE_DIR -type d -ctime +90的输出吗?它试图删除的目录似乎已被删除,因此无法引用指向它的符号链接。
  • 也许还使用-depth 有帮助?
  • 我需要做更多的测试,但-depth 似乎摆脱了错误。谢谢!

标签: shell unix


【解决方案1】:

试试

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

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

【讨论】:

  • 对不起。我忘了提到我正在使用 Korn shell。这是我得到的输出:find: 0652-017 -mindepth is not a valid option.
  • 我有点担心,如果你不使用 mindepth,整个目录可能会被删除!!!!
【解决方案2】:
find .$BASE_DIR -type d -ctime +90 | sort -r | xargs rm -rf

sort -r 将按照相反的顺序对我们的目录进行排序,因此我们不会尝试先删除外部目录然后再​​删除内部目录。

【讨论】:

    猜你喜欢
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    相关资源
    最近更新 更多