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

问题描述 投票:0回答:5

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

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

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

linux find delete-file
5个回答
79
投票

这对我有用:

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

38
投票

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

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

30
投票

这个其他答案会污染文件系统,并且

find
本身提供了“删除”选项。因此,我们不必将结果通过管道传输到 xargs,然后发出 rm。

这个答案更高效:

find /path -type f -not -newermt "YYYY-MM-DD HH:MI:SS" -delete

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

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


0
投票

首先我执行这个

touch -t 202310080000 /tmp/olddate ---> 这将创建一个具有指定日期的文件

找到“要应用搜索的路径”-输入 f ! -newer /tmp/olddate -delete -----> 这会参考在特定日期创建的文件从您想要的路径中删除文件。

© www.soinside.com 2019 - 2024. All rights reserved.