BASH - Delete files older than 3 months?

雨燕双飞 提交于 2021-02-04 13:39:47

问题


Delete files older than 3 months how?

For 90 days i know:

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 -mtime to follow months?


回答1:


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

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 /tmp/*.log -type f \
-mtime "+$(( ( $(date '+%s') - $(date -d '3 months ago' '+%s') ) / 86400 ))" -delete


来源:https://stackoverflow.com/questions/45838304/bash-delete-files-older-than-3-months

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!