5

所以我正在编写一个 bash 脚本来清除临时文件并遇到莫名其妙的行为。

# Find using mmin flag
find /usr/local/store/file/temp/3_day/ -mmin +$((60*24*3)) -type f > /tmp/old_files_by_mmin.txt

# Find using mtime flag
find /usr/local/store/file/temp/3_day/ -mtime +3 -type f > /tmp/old_files_by_mtime.txt

diff -u /tmp/old_files_by_mmin.txt /tmp/old_files_by_mtime.txt

前几行:

--- /tmp/old_files_by_mmin.txt  2016-08-03 16:56:42.535458820 +0000
+++ /tmp/old_files_by_mtime.txt 2016-08-03 16:56:58.310681524 +0000
@@ -117,59 +117,6 @@
/usr/local/store/file/temp/3_day/image/resize/2016/07/29/11/15/36/1296924350
/usr/local/store/file/temp/3_day/image/resize/2016/07/29/11/47/52/1950191632
/usr/local/store/file/temp/3_day/image/resize/2016/07/29/11/30/01/711250694
-/usr/local/store/file/temp/3_day/image/resize/2016/07/31/10/04/15/44313759
-/usr/local/store/file/temp/3_day/image/resize/2016/07/31/10/04/15/1589177813
-/usr/local/store/file/temp/3_day/image/resize/2016/07/31/10/04/15/1189074525
-/usr/local/store/file/temp/3_day/image/resize/2016/07/31/10/56/44/91382315
-/usr/local/store/file/temp/3_day/image/resize/2016/07/31/09/43/45/1622776054
-/usr/local/store/file/temp/3_day/image/resize/2016/07/31/01/44/57/1465920226
-/usr/local/store/file/temp/3_day/image/resize/2016/07/31/01/23/17/1467026748
-/usr/local/store/file/temp/3_day/image/resize/2016/07/31/01/15/58/1990201487
-/usr/local/store/file/temp/3_day/image/resize/2016/07/31/01/13/19/1990298215
-/usr/local/store/file/temp/3_day/image/resize/2016/07/31/01/35/59/518813467
-/usr/local/store/file/temp/3_day/image/resize/2016/07/31/12/10/53/1962045410
-/usr/local/store/file/temp/3_day/image/resize/2016/07/31/12/31/27/290517373
-/usr/local/store/file/temp/3_day/image/resize/2016/07/31/12/05/08/547481306

为什么 -mmin 标志会拾取 mtime 标志没有的文件?如果两者都应该找到比现在 + 3 天更旧的文件?

4

1 回答 1

1

鉴于实现之间的区别,有必要看看POSIX 标准的find授权:

-mtime n

如果从初始化时间中减去的文件修改时间除以 86400(丢弃任何余数)为 n,则主节点应评估为真。


同样,根据手册(对于 BSD 查找):

-mtime n[smhdw]

如果未指定单位,则如果文件上次修改时间和开始查找的时间之间的差异(向上舍入到下一个完整的 24 小时周期)为n24 小时周期,则此主要计算结果为 true。

...因此:在 BSDfind中,默认行为是四舍五入到完整的 24 小时周期。


对于 GNU find,有一个可配置性度量;见-daystart

-daystart

从今天开始而不是从 24 小时前开始测量时间(对于 -amin、-atime、-cmin、-ctime、-mmin 和 -mtime)。此选项仅影响稍后出现在命令行上的测试。

但是,默认行为在 的定义中给出-atime

-atime n

文件最后一次访问n*24 小时前。当find计算出上次访问该文件的时间是多少 24 小时前,任何小数部分都将被忽略,因此要匹配-atime +1,文件必须至少在两天前被访问过。

于 2016-08-03T17:49:13.137 回答