删除早于linux中特定日期的文件

问题描述 投票:21回答:3

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

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

但是,现在我想删除修改时间早于2014年1月1日的所有文件

我怎么在linux中做到这一点。

linux find delete-file
3个回答
20
投票

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

例如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.

25
投票

这对我有用:

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

1
投票
find ~ -type f ! -atime 4|xargs ls -lrt

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


1
投票

接受的答案会污染文件系统并发现自己提供删除。所以我们不必将结果传递给xargs然后发出rm。这个答案更有效率

find /path -type f -not -newermt "YYYY:MM:DD HH:MI:SS" -delete
© www.soinside.com 2019 - 2024. All rights reserved.