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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:46:40  来源:igfitidea点击:

How to delete files older than X hours

bash

提问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

Does your findhave the -mminoption? That can let you test the number of mins since last modification:

find-mmin选择吗?这可以让您测试自上次修改以来的分钟数:

find $LOCATION -name $REQUIRED_FILES -type f -mmin +360 -delete

Or maybe look at using tmpwatchto do the same job. phjr also recommended tmpreaperin the comments.

或者也许看看使用tmpwatch来做同样的工作。phjr也在tmpreaper评论中推荐。

回答by xtofl

You could to this trick: create a file 1 hour ago, and use the -newer fileargument.

您可以使用这个技巧:1 小时前创建一个文件,然后使用该-newer file参数。

(Or use touch -tto 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/crontabto 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 finddoes not have -mminand if one also is stuck with a findthat 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 findto 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 findprobably also has a less-than-capable datecommand 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.

更多类型。