如何删除 Linux 中特定日期之前的文件?
How do you delete files older than specific date in Linux?
我使用下面的命令删除了超过一年的文件。
find /path/* -mtime +365 -exec rm -rf {} \;
但现在我想删除所有修改时间早于 2014 年 1 月 1 日的文件。 如何在 Linux 中执行此操作?
您可以将您的时间戳作为一个文件来触摸,并将其用作参考点:
例如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 /path ! -newermt "YYYY-MM-DD HH:MM:SS" | xargs rm -rf
find ~ -type f ! -atime 4|xargs ls -lrt
这将列出访问的文件 早于 4 天,从主目录搜索。
污染了文件系统,find
本身提供了一个“删除”选项。因此,我们不必将结果通过管道传递给 xargs 然后发出 rm。
这个回答更有效率:
find /path -type f -not -newermt "YYYY-MM-DD HH:MI:SS" -delete
我使用下面的命令删除了超过一年的文件。
find /path/* -mtime +365 -exec rm -rf {} \;
但现在我想删除所有修改时间早于 2014 年 1 月 1 日的文件。 如何在 Linux 中执行此操作?
您可以将您的时间戳作为一个文件来触摸,并将其用作参考点:
例如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 /path ! -newermt "YYYY-MM-DD HH:MM:SS" | xargs rm -rf
find ~ -type f ! -atime 4|xargs ls -lrt
这将列出访问的文件 早于 4 天,从主目录搜索。
find
本身提供了一个“删除”选项。因此,我们不必将结果通过管道传递给 xargs 然后发出 rm。
这个回答更有效率:
find /path -type f -not -newermt "YYYY-MM-DD HH:MI:SS" -delete