Bash script delete files older than n days ago based on the timestamp in filename? [duplicate]

筅森魡賤 提交于 2019-12-31 05:43:33

问题


I have some logs files with

 somefiles.log.YYYY-mm-dd

and I want to delete those files that are older than N days base on the timestamp in its filename.


回答1:


use cut command to retrieve YYYY-MM-dd part of filename. and use date command as following to convert it to time_t type.

date -d "YYYY-MM-dd" +%s

then you can compare time_t to determine which file should be deleted.




回答2:


Better delete based on creation time using find:

find /var/log/ -name somefiles.log.* -ctime +3 -delete


来源:https://stackoverflow.com/questions/35353801/bash-script-delete-files-older-than-n-days-ago-based-on-the-timestamp-in-filenam

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