Bash script delete files older than n days ago bas

2019-09-05 04:18发布

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.

2条回答
萌系小妹纸
2楼-- · 2019-09-05 05:09

Better delete based on creation time using find:

find /var/log/ -name somefiles.log.* -ctime +3 -delete
查看更多
Summer. ? 凉城
3楼-- · 2019-09-05 05:20

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.

查看更多
登录 后发表回答