Bash脚本根据文件名中的时间戳删除早于n天的文件? [英] Bash script delete files older than n days ago based on the timestamp in filename?
本文介绍了Bash脚本根据文件名中的时间戳删除早于n天的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些带有
somefiles.log.YYYY-mm-dd
我想根据文件名中的时间戳删除那些超过 N 天的文件.
and I want to delete those files that are older than N days base on the timestamp in its filename.
推荐答案
使用 cut 命令获取文件名的 YYYY-MM-dd 部分.并使用如下 date 命令将其转换为 time_t 类型.
use cut command to retrieve YYYY-MM-dd part of filename. and use date command as following to convert it to time_t type.
日期 -d "YYYY-MM-dd" +%s
date -d "YYYY-MM-dd" +%s
然后您可以比较 time_t 以确定应该删除哪个文件.
then you can compare time_t to determine which file should be deleted.
这篇关于Bash脚本根据文件名中的时间戳删除早于n天的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文