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屋!
查看全文