【问题标题】:Keep the recent 3 folders and delete the rest in bash script?保留最近的 3 个文件夹并在 bash 脚本中删除其余文件夹?
【发布时间】:2014-04-04 08:42:36
【问题描述】:

我是 shell 脚本的新手。你能建议我一些代码来满足以下要求吗?

我有以下格式化文件夹

示例:/home/backup/store_id/datewisefolder/some.zip

喜欢:/home/backup/44/22032014/some_file.zip

  /home/backup/44/23032014/some_file.zip 

  /home/backup/44/24032014/some_file.zip 

   /home/backup/44/25032014/some_file.zip

还有很多..

我想去每个商店 id 文件夹并只保留最近的 3 个日期明智的文件夹其余部分已删除。这里有 44 个商店 id 文件夹 23032014,24032014,25032014 这三个是最近的一个,所以保持原样。 22032014 旧,所以删除一个。

我编写了查找最近三个文件的 shell 代码,但我不知道如何使用 store_ID 文件夹循环删除其余文件。

下面的代码找出最近的文件夹日期

cd /home/backup/44/ ls -1 |排序-n -k1.8 -k1.4 -k 1 |尾 -3

【问题讨论】:

标签: linux bash shell sh


【解决方案1】:
ls /home/backup | while read store_id
do
  count=0
  ls -t /home/backup/$store_id | while read dir_to_remove
  do
    count=$((count + 1))
    if [ $count -gt 3 ]; then
      rm -rf /home/backup/$store_id/$dir_to_remove
    fi
  done
done

【讨论】:

    【解决方案2】:

    find 是这类任务的常用工具:

    find ./my_dir -mtime +3 -delete
    
    explaination:
    
    ./my_dir your directory (replace with your own)
    -mtime +3 older than 3 days
    -delete delete it.
    

    或根据您的脚本代码

    files=(`ls -1 /my_dir | sort -n -k1.8 -k1.4 -k 1 | tail -3`)
    
    for i in *; do 
     keep=0; 
     #Check whether this file is in files array:
     for a in ${files[@]}; do 
      if [ $i == $a ]; then 
       keep=1; 
      fi; 
     done; 
     # If it wasn't, delete it
     if [ $keep == 0 ]; then 
      rm -rf $i;
     fi;
    done
    

    【讨论】:

    • 我没有固定的 /my_dir 文件夹。就像 /home/backup/44,43,56,77,----在那个日期明智的文件夹中,我需要旋转的文件夹。
    • @user2481010 因此,通过在您的所有目录中提供路径(如/home/backup/44,43,56,77,)来使用第一个解决方案。
    • 您也可以在第二个解决方案命令中使用find /home/backup -type d 之类的命令而不是ls 来完成您的工作。
    【解决方案3】:

    下面的脚本可以工作。

    declare -a axe
    axe=`ls -l /home/backup/* | sort -n -k1.8 -k1.4 -k 1|head -n -3`
    
    for i in $axe
    do
      rm -rf $i;
    done
    

    【讨论】:

    • 44 store_id 不是常量文件夹,它有更多的 storeid,例如 45,46,47,87,98..等等
    【解决方案4】:

    cd 依次进入每个 store_id 目录。创建一个八位数名称的目录列表,按倒序排列(最近的在前);最后将列表的一部分(省略第一个 $retain 元素)传递给 rm。

    basedir=/home/backup
    # How many directories to keep:
    retain=3
    
    shopt -s nullglob
    
    # Assuming that anything beginning with a digit is a store_id directory.
    for workdir in "$basedir"/[0-9]*/ ; do
      (
        cd "$workdir" || exit 1
        # Create the list.  The nullglob option ensures that if there are no
        # suitable directories, rmlist will be empty.
        rmlist=(
            $(
              printf '%s\n' [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/ |
              sort -nr -k1.5,1.8 -k1.3,1.4 -k1.1,1.2
            )
          )
        # If there are fewer than $retain entries in ${rmlist[@]}, rm will be
        # given no names and so do nothing.
        rm -rf -- "${rmlist[@]:$retain}"
      )
    done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多