【问题标题】:Delete all files in a directory, except those listed matching specific criteria删除目录中的所有文件,但列出的匹配特定条件的文件除外
【发布时间】:2012-09-09 14:27:56
【问题描述】:

我需要自动清理仅保存备份文件的基于 Linux 的 FTP 服务器。

在我们的“\var\DATA”目录中是一个目录集合。此处用于备份的任何目录都以“DEV”开头。每个“DEVxxx*”目录中都有实际的备份文件,以及在这些设备的维护过程中可能需要的任何用户文件。

我们只想保留以下文件 - 在这些“DEVxxx*”目录中发现的任何其他内容都将被删除:

The newest two backups:  ls -t1 | grep -m2 ^[[:digit:]{6}_Config]  
The newest backup done on the first of the month:  ls -t1 | grep -m1 ^[[:digit:]{4}01_Config] 
Any file that was modified less than 30 days ago:  find -mtime -30  
Our good configuration file:  ls verification_cfg

任何与上述不符的都应删除。

我们如何编写这个脚本?

我猜 BASH 脚本可以做到这一点,我们可以创建一个每天运行的 cron 作业来执行任务。

【问题讨论】:

    标签: linux bash delete-file rm


    【解决方案1】:

    不管怎样,这里是我为完成任务而创建的 bash 脚本。欢迎评论。

    #!/bin/bash
    
    # This script follows these rules:
    #
    #  - Only process directories beginning with "DEV"
    #  - Do not process directories within the device directory
    #  - Keep files that match the following criteria:
    #     - Keep the two newest automated backups
    #     - Keep the six newest automated backups generated on the first of the month
    #     - Keep any file that is less than 30 days old
    #     - Keep the file "verification_cfg"
    #
    #  - An automated backup file is identified as six digits, followed by "_Config"
    #    e.g.  20120329_Config
    
    
    # Remember the current directory
    CurDir=`pwd`
    
    # FTP home directory
    DatDir='/var/DATA/'
    cd $DatDir
    
    # Only process directories beginning with "DEV"
    for i in `find . -type d -maxdepth 1 | egrep '\.\/DEV' | sort` ; do
     cd $DatDir
    
     echo Doing "$i"
     cd $i
    
     # Set the GROUP EXECUTE bit on all files
     find . -type f -exec chmod g+x {} \;
    
     # Find the two newest automated config backups
     for j in `ls -t1 | egrep -m2 ^[0-9]{8}_Config$` ; do
      chmod g-x $j
     done
    
     # Find the six newest automated config backups generated on the first of the month
     for j in `ls -t1 | egrep -m6 ^[0-9]{6}01_Config$` ; do
      chmod g-x $j
     done
    
     # Find all files that are less than 30 days old
     for j in `find -mtime -30 -type f` ; do
      chmod g-x $j
     done
    
     # Find the "verification_cfg" file
     for j in `find -name verification_cfg` ; do
      chmod g-x $j
     done
    
     # Remove any files that still have the GROUP EXECUTE bit set
     find . -type f -perm -g=x -exec rm -f {} \;
    
    done
    
    # Back to the users current directory
    cd $CurDir
    

    【讨论】:

      【解决方案2】:

      大概是这样的吧?

      { ls -t1 | grep -m2 ^[[:digit:]{6}_Config] ;
        ls -t1 | grep -m1 ^[[:digit:]{4}01_Config] ;
        find -mtime -30 ;
        ls -1 verification_cfg ;
      } | rsync -a --exclude=* --include-from=- /var/DATA/ /var/DATA.bak/
      rm -rf /var/DATA
      mv /var/DATA.bak /var/DATA
      

      【讨论】:

      • 看起来不错,除了在此过程中,我们尝试访问我们想要的文件之一的可能性很小,它只会存在于 /var/DATA.bak 中。除了列出的文件之外,没有办法 rm 所有文件吗?
      • 试试rsync -a --delete /var/DATA.bak/ /var/DATA/ 而不是rm -rf /var/DATA
      猜你喜欢
      • 2010-10-26
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 2014-04-27
      • 2015-11-27
      • 2021-11-24
      • 1970-01-01
      • 2016-01-19
      相关资源
      最近更新 更多