Linux 删除超过 X 分钟的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17763415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:19:02  来源:igfitidea点击:

Delete files older than X minutes

linuxbashcommand-line

提问by Abs

I would like to delete files that are older than 59 minutes. I have this so far:

我想删除超过 59 分钟的文件。到目前为止我有这个:

find /tmp -daystart -maxdepth 1 -mmin +59 -type f -name "*.*" -exec rm -f {} \;

This doesn't work and seems to delete all files. I've tested this several times and I think the issue is to do with daystart.

这不起作用,似乎删除了所有文件。我已经对此进行了多次测试,我认为问题与daystart.

I've read the man page and it seems to base time on the beginning of the day rather than from 24 hours ago. If this is the case how can I accurately delete files that are older than 59 minutes? Do I need to account for daystartand add some more minutes?

我已经阅读了手册页,它似乎基于一天的开始而不是 24 小时前的时间。如果是这种情况,我如何才能准确删除超过 59 分钟的文件?我需要考虑daystart并添加更多分钟吗?

Example:

例子:

ubuntu@ip-10-138-30-118:/tmp$ ls -la
total 8
drwxrwxrwt  2 root   root   4096 Jul 20 14:39 ./
drwxr-xr-x 23 root   root   4096 Jun 25 18:34 ../
-rw-rw-r--  1 ubuntu ubuntu    0 Jul 20 12:35 a.txt

Both the following commands, return the file:

以下两个命令都返回文件:

ubuntu@ip-10-138-30-118:/tmp$ find /tmp -daystart -maxdepth 1 -mmin +59 -type f -name "*.*"
/tmp/a.txt

And:

和:

ubuntu@ip-10-138-30-118:/tmp$ find /tmp -daystart -maxdepth 1 -mmin +359 -type f -name "*.*"
/tmp/a.txt

However, the file is not older than 659 minutes (10.9 hours)! But at 759 (12.65 hours), it doesn't return the file anymore?

但是,该文件不超过 659 分钟(10.9 小时)!但是在 759(12.65 小时)时,它不再返回文件?

采纳答案by Barmar

When used with -mmin, -daystartappears to make it calculate from the endof today, not the beginning.

与 一起使用时-mmin-daystart似乎使它从今天结束而不是开始计算。

If you just want to find files modified more than 59 minutes ago, you don't need that option. -mmincalculates from the current time by default.

如果您只想查找 59 分钟前修改的文件,则不需要该选项。-mmin默认从当前时间开始计算。

barmar@dev:~/testdir$ date
Sat Jul 20 10:02:20 CDT 2013
barmar@dev:~/testdir$ ls -l
total 0
-rw-r--r-- 1 barmar adm 0 Jul 20 09:57 a.txt
barmar@dev:~/testdir$ find . -maxdepth 1 -mmin +2 -type f
./a.txt
barmar@dev:~/testdir$ find . -maxdepth 1 -mmin +10 -type f

回答by matson kepson

this should work for you

这应该适合你

find /path -mmin +59 -type f -exec rm -fv {} \;

find /path -mmin +59 -type f -exec rm -fv {} \;