【问题标题】:How to Delete files in Linux with to the date in the file name?如何在 Linux 中删除文件名中包含日期的文件?
【发布时间】:2023-01-10 02:07:55
【问题描述】:

我的 Linux VM 服务器中有一个名为 /var/test/logs 的文件夹。它有如下文件夹(示例):

drwxr-xr-x 3 test test 26 Dec 01 00:00 Backuplogs_202212010000
drwxr-xr-x 3 test test 26 Dec 02 00:00 Backuplogs_202212020000
drwxr-xr-x 3 test test 26 Dec 12 00:00 Backuplogs_202212120000
drwxr-xr-x 3 test test 26 Dec 13 00:00 Backuplogs_202212130000
drwxr-xr-x 3 test test 26 Dec 14 00:00 Backuplogs_202212140000
drwxr-xr-x 3 test test 26 Dec 15 00:00 Backuplogs_202212150000

我需要删除除每个月的第 1 天、第 15 天和最后一天以外的所有文件夹,方法是使用只要上面给出的文件夹名称如下所示,

备份日志_202212010000

计算每个月的日期以删除除第 1 天、第 15 天和最后一天之外的文件夹

有人可以建议我这样做的逻辑吗?

【问题讨论】:

    标签: linux logging delete-file


    【解决方案1】:

    转到您的路径/var/test/logs,并尝试删除当月的这个:

    #Return last date of the current month
    LAST=$(date -d "-$(date +%d) days +1 month" +%d)
    
    #Remove all folders except 01, 15, 31
    find . -regextype egrep ! -regex "^./Backuplogs_202301(01|15|${LAST}).*"  -delete
    

    如果你想要几个月,那么你可以根据你的范围在上面添加for-loop

    【讨论】:

      猜你喜欢
      • 2017-01-13
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 2019-04-11
      • 2010-11-12
      相关资源
      最近更新 更多