简体   繁体   English

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

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

I used the below command to delete files older than a year.我使用以下命令删除了超过一年的文件。

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

But now I want to delete all files whose modified time is older than 01 Jan 2014. How do I do this in Linux?但是现在我想删除所有修改时间早于2014 年 1 月 1 日的文件。我如何在 Linux 中执行此操作?

这对我有用:

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

You can touch your timestamp as a file and use that as a reference point:您可以将时间戳作为文件触摸,并将其用作参考点:

eg for 01-Jan-2014:例如 2014 年 1 月 1 日:

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

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

this works because find has a -newer switch that we're using.这是有效的,因为find有一个我们正在使用的-newer开关。

From man find :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.

This other answer pollutes the file system and find itself offers a "delete" option.这个另一个答案污染了文件系统,并find它自己提供了一个“删除”选项。 So, we don't have to pipe the results to xargs and then issue an rm.因此,我们不必将结果通过管道传输到 xargs 然后发出 rm。

This answer is more efficient:这个答案更有效:

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.

相关问题 如何删除 LINUX 中超过 30 天且具有特定扩展名 *.pdf 的文件 - How to delete files in LINUX which are older than 30 days and are of specific extension *.pdf 如何查找和删除早于 x 的文件,但始终在 linux 中保留一个(synology) - How do I find and delete files older than x but always leave one in linux (synology) linux中如何删除超过n天的文件和目录 - How to delete files and directories older than n days in linux Linux 删除早于同一时间的文件 - Linux Delete Files Older Than and the Same Time Smaller Than Linux:使用一个特殊文件删除每个早于日期的文件 - Linux: Delete every file older than a date with one exceptional file 你如何杀死所有超过某个年龄的 Linux 进程? - How do you kill all Linux processes that are older than a certain age? 在Linux中,如何查找修改日期不早于给定文件的文件? - In Linux, how to find files with modification dates NOT older than a given file? 如何使用 PHP 删除超过 7 天的文件? - How Can I Delete Files Older Than 7 Days Using PHP? 如何使用文件名中的日期查找一个月以上的文件? - How to find files older than a month using date in filename? 删除文件名早于某些天且名称中没有特定子字符串的文件(Linux) - Delete files older than certain days that not have certain substring in name (Linux)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM