繁体   English   中英

如何在 Linux 中删除早于特定日期的文件?

[英]How do you delete files older than specific date in Linux?

我使用以下命令删除了超过一年的文件。

  find /path/* -mtime +365 -exec rm -rf {} \;

但是现在我想删除所有修改时间早于2014 年 1 月 1 日的文件。我如何在 Linux 中执行此操作?

这对我有用:

find /path ! -newermt "YYYY-MM-DD HH:MM:SS" | xargs rm -rf

您可以将时间戳作为文件触摸,并将其用作参考点:

例如 2014 年 1 月 1 日:

touch -t 201401010000 /tmp/2014-Jan-01-0000

find /path -type f ! -newer /tmp/2014-Jan-01-0000 | xargs rm -rf 

这是有效的,因为find有一个我们正在使用的-newer开关。

man find

-newer file
       File  was  modified  more  recently than file.  If file is a symbolic
       link and the -H option or the -L option is in effect, the modification time of the 
       file it points to is always used.

这个另一个答案污染了文件系统,并find它自己提供了一个“删除”选项。 因此,我们不必将结果通过管道传输到 xargs 然后发出 rm。

这个答案更有效:

find /path -type f -not -newermt "YYYY-MM-DD HH:MI:SS" -delete
find ~ -type f ! -atime 4|xargs ls -lrt

这将列出访问时间超过 4 天的文件,从主目录搜索。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM