BASH - 删除超过 3 个月的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45838304/
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-18 16:24:24  来源:igfitidea点击:

BASH - Delete files older than 3 months?

linuxbashcentosfinddelete-file

提问by anubhava

Delete files older than 3 months how?

如何删除超过 3 个月的文件?

For 90 days i know:

90 天我知道:

find /tmp/*.log -mtime +90 -type f -delete

But how do i know 3 months equal to always 90 days? how many exact days? Is there more better way to tell the -mtimeto follow months?

但是我怎么知道 3 个月等于 90 天?准确的天数是多少?有没有更好的方法告诉-mtime跟随months

回答by anubhava

If you want exact number of days for 3 months then you can use:

如果您想要 3 个月的确切天数,则可以使用:

days=$(( ( $(date '+%s') - $(date -d '3 months ago' '+%s') ) / 86400 ))

and use it as:

并将其用作:

find /tmp/*.log -mtime +$days -type f -delete

Or directly in find:

或直接在find

find /tmp/*.log -type f \
-mtime "+$(( ( $(date '+%s') - $(date -d '3 months ago' '+%s') ) / 86400 ))" -delete