bash 如何删除超过 X 小时的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/249578/
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
How to delete files older than X hours
提问by Tom Feiner
I'm writing a bash script that needs to delete old files.
我正在编写一个需要删除旧文件的 bash 脚本。
It's currently implemented using :
它目前使用:
find $LOCATION -name $REQUIRED_FILES -type f -mtime +1 -delete
This will delete of the files older than 1 day.
这将删除早于 1 天的文件。
However, what if I need a finer resolution that 1 day, say like 6 hours old? Is there a nice clean way to do it, like there is using find and -mtime?
但是,如果我需要 1 天的更精细分辨率,比如 6 小时前,该怎么办?有没有一种很好的干净的方法来做到这一点,就像使用 find 和 -mtime 一样?
回答by Paul Dixon
回答by xtofl
You could to this trick: create a file 1 hour ago, and use the -newer file
argument.
您可以使用这个技巧:1 小时前创建一个文件,然后使用该-newer file
参数。
(Or use touch -t
to create such a file).
(或用于touch -t
创建此类文件)。
回答by Axel Ronsin
Here is the approach that worked for me (and I don't see it being used above)
这是对我有用的方法(我没有看到上面使用它)
$ find /path/to/the/folder -name *.* -mmin +59 -delete > /dev/null
deleting all the files older than 59 minutes while leaving the folders intact.
删除所有早于 59 分钟的文件,同时保持文件夹完好无损。
回答by Rajeev Rumale
For SunOS 5.10
对于 SunOS 5.10
Example 6 Selecting a File Using 24-hour Mode
The descriptions of -atime, -ctime, and -mtime use the ter-
minology n ``24-hour periods''. For example, a file accessed
at 23:59 is selected by:
example% find . -atime -1 -print
at 00:01 the next day (less than 24 hours later, not more
than one day ago). The midnight boundary between days has no
effect on the 24-hour calculation.
回答by Eragonz91
find $PATH -name $log_prefix"*"$log_ext -mmin +$num_mins -exec rm -f {} \;
find $PATH -name $log_prefix"*"$log_ext -mmin +$num_mins -exec rm -f {} \;
回答by satyr0909
Here is what one can do for going on the way @iconoclast was wondering about in their commenton another answer.
以下是@iconoclast 在他们对另一个答案的评论中所想的那样,人们可以做些什么。
use crontab for user or an /etc/crontab
to create file /tmp/hour
:
使用 crontab 为用户或 an/etc/crontab
创建文件/tmp/hour
:
# m h dom mon dow user command
0 * * * * root /usr/bin/touch /tmp/hour > /dev/null 2>&1
and then use this to run your command:
然后使用它来运行您的命令:
find /tmp/ -daystart -maxdepth 1 -not -newer /tmp/hour -type f -name "for_one_hour_files*" -exec do_something {} \;
回答by Malcolm Boekhoff
If you do not have "-mmin" in your version of "find", then "-mtime -0.041667" gets pretty close to "within the last hour", so in your case, use:
如果您的“查找”版本中没有“-mmin”,则“-mtime -0.041667”非常接近“在最后一小时内”,因此在您的情况下,请使用:
-mtime +(X * 0.041667)
so, if X means 6 hours, then:
因此,如果 X 表示 6 小时,则:
find . -mtime +0.25 -ls
works because 24 hours * 0.25 = 6 hours
工作是因为 24 小时 * 0.25 = 6 小时
回答by kbulgrien
If one's find
does not have -mmin
and if one also is stuck with a find
that accepts only integer values for -mtime
, then all is not necessarily lost if one considers that "older than" is similar to "not newer than".
如果一个find
人没有,-mmin
并且如果一个人也坚持find
只接受 的整数值-mtime
,那么如果认为“旧于”类似于“不更新于”,则不一定会丢失所有内容。
If we were able to create a file that that has an mtime of our cut-off time, we can ask find
to locate the files that are "not newer than" our reference file.
如果我们能够创建一个具有我们截止时间的 mtime 的文件,我们可以要求find
定位“不比”我们的参考文件更新的文件。
To create a file that has the correct time stamp is a bit involved because a system that doesn't have an adequate find
probably also has a less-than-capable date
command that could do things like: date +%Y%m%d%H%M%S -d "6 hours ago"
.
创建一个具有正确时间戳的文件有点复杂,因为一个没有足够时间戳的系统find
可能也有一个能力不足的date
命令,可以执行以下操作: date +%Y%m%d%H%M%S -d "6 hours ago"
.
Fortunately, other old tools that can manage this, albeit in a more unwieldy way.
幸运的是,其他旧工具可以管理这一点,尽管方式更加笨拙。
Consider that six hours is 21600 seconds. We want to find the time that is six hours ago in a format that is useful:
考虑六个小时是 21600 秒。我们想以一种有用的格式找到六小时前的时间:
$ date && perl -e '@d=localtime time()-21600; \
printf "%4d%02d%02d%02d%02d.%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]'
> Thu Apr 16 04:50:57 CDT 2020
202004152250.57
The perl statement did produce a useful date, but it has to be put to better use:
perl 语句确实产生了一个有用的日期,但必须更好地使用它:
$ date && touch -t `perl -e '@d=localtime time()-21600; \
printf "%4d%02d%02d%02d%02d.%02d\n", \
$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]'` ref_file && ls -l ref_file
Thu Apr 16 04:53:54 CDT 2020
-rw-rw-rw- 1 root sys 0 Apr 15 22:53 ref_file
Now the solution for this old UNIX is something along the lines of:
现在这个旧 UNIX 的解决方案是:
$ find . -type f ! -newer ref_file -a ! -name ref_file -exec rm -f "{}" \;
It might also be a good idea to clean up our reference file...
清理我们的参考文件也可能是一个好主意......
$ rm -f ref_file
回答by GavinCattell
-mmin is for minutes.
-mmin 是分钟。
Try looking at the man page.
尝试查看手册页。
man find
for more types.
更多类型。