had*_*des 3 linux shell date globbing
我的文件名称以 YYYYMMDD 开头,如下所示:
20160221_143223_Report.csv
20160222_121223_Report.csv
...
20160229_141223_Report.csv
20160301_171223_Report.csv
Run Code Online (Sandbox Code Playgroud)
我想要 tar 文件名中包含“Report”且其名称对应于 1-8 天前范围的文件。例如:今天是 3 月 1 日,所以我想 tar 2 月 22 日到 2 月 29 日(不包括今天)的文件。
files=($(ls |<HAS "Report" in filename> and <WITHIN `date -d "8 days ago" '+%Y%m%d'> )) #output will be filenames that has the word "Report" and from 22th to 29th FEB
tar cvfz oldfile.tar.gz "${files[@]}"
Run Code Online (Sandbox Code Playgroud)
我应该匹配什么模式?
find如果您想自己运行它可能是最好的选择:
find . -name '*Report.csv' -mtime +8 | xargs tar -czvf oldfile.tar.gz
Run Code Online (Sandbox Code Playgroud)
还有一个非常标准的工具用于清除旧文件,称为logrotate它可以匹配文件夹中的模式并执行各种整理,包括压缩旧文件和删除历史记录。