如何根据文件名中的日期删除超过 30 天的文件 [英] How to delete files older than 30 days based on the date in the filename

查看:44
本文介绍了如何根据文件名中的日期删除超过 30 天的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每天都会更新 CSV 文件,我们会根据文件名中的日期处理文件并删除超过 30 天的文件.示例文件名:

I have CSV files get updated every day and we process the files and delete the files older than 30 days based on the date in the filename. Example filenames :

XXXXXXXXXXX_xx00xx_**20171001**.000000_0.csv

我想在 crontab 中安排作业,每天删除 30 天以前的文件.

I would like to schedule the job in crontab to delete 30 days older files daily.

路径可以是 /mount/store/XXXXXXXXXXXX_xx00xx_**20171001**.000000_0.csv

if [ $(date -d '-30 days' +%Y%m%d) -gt $D ]; then
    rm -rf $D
fi

上面的脚本似乎对我没有帮助.请帮我解决这个问题.过去两天我一直在尝试这个.

this above script doesn't seem to help me. Kindly help me on this. I have been trying this for last two days.

使用 CENTOS7

谢谢.

推荐答案

以下方法不查看文件的任何生成时间信息,它假定文件名中的日期与文件创建日期无关.

The following approach does not look at any generation time information of the file, it assumes the date in the filename is unrelated to the day the file is created.

#/usr/bin/env bash
d=$(date -d "-30 days" "+%Y%m%d")
for file in /yourdir/*csv; do
     date=${file:$((${#file}-21)):8}
     (( date < d )) && rm $file
done

这篇关于如何根据文件名中的日期删除超过 30 天的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆