【问题标题】:Bash command to delete everything older than half a dayBash命令删除超过半天的所有内容
【发布时间】:2012-11-12 10:42:03
【问题描述】:

我有一个简单的 bash 脚本,在每晚午夜运行,它会创建一个备份或文件,并将它们作为 .tar.gz 存储在我的 Dropbox 中。然而,在这发生之前,我需要脚本来删除前一天晚上的备份。

为此,我目前正在运行以下命令:

find ~/Dropbox/Backups/casper/* -mtime +0.5 -exec rm {} \;

在我看来应该删除半天以上的任何内容 - 但它似乎不起作用(它保留前几个晚上的备份,但删除之前的任何内容)

有人能指出我正确的方向吗?谢谢

【问题讨论】:

  • -0.5 会帮助你吗? (未测试)。
  • 我什至不能使用句点字符“find: -mtime: +0.5: bad unit '.'”。它适用于“find dir -mtime +12h”。

标签: bash


【解决方案1】:

来自find 的手册页:

-mtime n
          File's data was last modified n*24 hours ago.  See the comments for -atime to understand how rounding  affects  the
          interpretation of file modification times.

-atime n
          File  was  last  accessed  n*24  hours  ago.   When find figures out how many 24-hour periods ago the file was last
          accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days
          ago.

由此我们可以看出 0.5 被去掉了,那么 1 day ago 是必需的。您可能想改用-mmin

例如(来自babah):

# 720 is 60 times 12
find ~/Dropbox/Backups/casper/* -mmin 720 -print -exec rm {} \;

【讨论】:

  • +1,以 mmin 为例:find ~/Dropbox/Backups/casper/* -mmin +720 -print -exec rm {} \;
猜你喜欢
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多