【问题标题】:Bash script to remove files older than 15 months用于删除超过 15 个月的文件的 Bash 脚本
【发布时间】:2014-04-27 21:11:35
【问题描述】:

在 bash 脚本中,如果我想删除目录中超过 15 天的文件,我可以运行:

find "$DIR" -type f -mtime +15 -exec rm {} \;

有人可以帮助我使用 bash 脚本来删除目录中超过 15 个月的文件吗?

这和 Bash 中的 ctime 一样吗?

【问题讨论】:

  • 但那是 1 个月。我没有看到类似于我想要的解决方案
  • -mtime +65w 可能持续 65 周,基于 1 年是 52 周,因此一年的四分之一是 13 周,52+13 = 65

标签: bash shell


【解决方案1】:

根据手册页:

 -mtime n[smhdw]
         If no units are specified, this primary evaluates to true if the difference between the file last modification time and the time find was started, rounded up to the next
         full 24-hour period, is n 24-hour periods.

         If units are specified, this primary evaluates to true if the difference between the file last modification time and the time find was started is exactly n units.  Please
         refer to the -atime primary description for information on supported time units.

然后,-atime:

 -atime n[smhdw]
         If no units are specified, this primary evaluates to true if the difference between the file last access time and the time find was started, rounded up to the next full
         24-hour period, is n 24-hour periods.

         If units are specified, this primary evaluates to true if the difference between the file last access time and the time find was started is exactly n units.  Possible
         time units are as follows:

         s       second
         m       minute (60 seconds)
         h       hour (60 minutes)
         d       day (24 hours)
         w       week (7 days)

         Any number of units may be combined in one -atime argument, for example, ``-atime -1h30m''.  Units are probably only useful when used in conjunction with the + or - modi-
         fier.

所以我们有几周的时间。 15 个月 * 4 周/月 = 60 周。

find $DIR -type f -mtime +60w -exec rm {} \;

【讨论】:

  • 如果我使用 ctime,+60w 是否相同?
  • 请注意,15 个月可能更像是 65 周......基于 1 年是 52 周,因此一年的四分之一是 13 周,52+13 = 65
  • @user3409351:是的,格式是一样的。可以在手册页中阅读...
【解决方案2】:

使用 450 (=15*30) 作为-mtime 参数。

find $DIR -type f -mtime +450 -exec rm {} \;

【讨论】:

  • 如果我使用 ctime,+450 是否相同?
【解决方案3】:

http://linux.die.net/man/8/tmpwatch 专为此应用而设计。通常与 cron 一起使用。

【讨论】:

    【解决方案4】:

    一个有趣的可能性:你可以touch一个带有时间戳15 months ago的tmp文件并将它与find的(否定)-newer标志一起使用:

    a=$(mktemp)
    touch -d '15 months ago' -- "$a"
    find "$DIR" -type f \! -newer "$a" -exec rm {} +
    rm -- "$a"
    

    当然,这假设您的 touchfind 具有这些功能。

    如果mktemp 有可能在您的目录$DIR 的子目录中创建文件,它会变得非常混乱,因为“$a”引用的文件可能会在进程结束之前被删除。在这种情况下,为了 100% 确定,请使用(否定的)-samefile 测试:

    find "$DIR" -type f \! -newer "$a" \! -samefile "$a" -exec rm {} +
    

    如果您的find 支持,您当然可以使用find-delete 命令。那将给出:

    a=$(mktemp)
    touch -d '15 months ago' -- "$a"
    find "$DIR" -type f \! -newer "$a" \! -samefile "$a" -delete
    rm -- "$a"
    

    【讨论】:

      猜你喜欢
      • 2018-01-31
      • 1970-01-01
      • 2020-09-25
      • 2015-05-27
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 2014-07-08
      相关资源
      最近更新 更多