【问题标题】:Delete only Directories (not files) older than x days in Linux [duplicate]在 Linux 中仅删除 x 天之前的目录(而非文件)[重复]
【发布时间】:2017-01-03 20:26:25
【问题描述】:

我正在尝试列出早于 x 天的目录(但不是文件)。我使用下面的命令来做到这一点。但它列出了目录和文件。谁能帮我解决它?谢谢。

find /path/to/base/dir/* -type d -ctime +10 -exec ls {} \;

【问题讨论】:

  • 它列出了目录和文件,因为您对结果说ls {}。相反,请使用ls -d {} 列出目录本身。
  • GNU find 有一个 -ls 操作,其行为类似于 ls -dils 对于 find 发现的内容。
  • 如果你只是想让ls -d这样的输出显示出来,你可以去掉这个动作,默认为-print,和-exec ls -d {} \;一样

标签: linux bash shell find gnu-findutils


【解决方案1】:

试试这个

find /path/to/base/dir -maxdepth 1 -type d -ctime +10 -exec rm -r {} \;

【讨论】:

    【解决方案2】:

    首先测试是否一切正常:

    find /path/to/base/dir -type d -ctime +10 -exec ls -d {} \;
    

    一切正常时:

    find /path/to/base/dir -type d -ctime +10 -exec rm -fr {} \;
    

    解释:

    ls -d :  list directories themselves, not their contents
    rm -f :  ignore nonexistent files and arguments, never prompt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 2023-03-20
      • 2018-05-01
      • 2016-03-28
      • 2016-11-16
      • 2013-05-16
      • 1970-01-01
      相关资源
      最近更新 更多