用 mtime - 和 + 找出不同之处

Gri*_*ckz 8 linux bash find

mtime's-+switch有什么区别,因为两者都没有带回我需要的结果?

我想删除所有超过 5 天的文件:

 find /mnt/sdb1/tmp/ -type f -mtime +5 -exec ls {} \;
 find /mnt/sdb1/tmp/ -type f -mtime -5 -exec ls {} \;
Run Code Online (Sandbox Code Playgroud)

我已将输出更改为ls以比较结果。

ter*_*don 14

find的手册页:

    Numeric arguments can be specified as

   +n     for greater than n,
   -n     for less than n,
    n     for exactly n.

  -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.
Run Code Online (Sandbox Code Playgroud)

所以,-mtime +5会发现最后修改这些文件超过5 * 24小时以前,-mtime -5将查找这些文件最近修改较少比5 * 24小时前。要删除超过 5 天1 的文件,您可以执行以下操作:

find /mnt/sdb1/tmp/ -type f -mtime +5 -exec rm {} \;
Run Code Online (Sandbox Code Playgroud)

如果这没有返回您想要的结果,则时间戳可能有问题。是否正确报告了相关文件?如果这是一个外部 USB 驱动器,文件可能是在另一台机器上创建的,并且具有与您预期不同的时间戳。


1请注意,这里的单位是一天,24 小时。所以超过 5 天意味着至少有 6 天,因为该值总是四舍五入并且忽略小数部分。