Linux 在bash脚本中保留最近的3个文件夹并删除其余文件夹?

Linux 在bash脚本中保留最近的3个文件夹并删除其余文件夹?,linux,bash,shell,sh,Linux,Bash,Shell,Sh,我对shell脚本是新手。你能为我推荐一些代码来满足以下要求吗 我有以下格式化文件夹 示例:/home/backup/store\u 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/

我对shell脚本是新手。你能为我推荐一些代码来满足以下要求吗

我有以下格式化文件夹

示例:/home/backup/store\u 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 store id文件夹230320142403201425032014这三个是最近的一个,请保持原样。22032014旧的,因此删除一个

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

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

cd/home/backup/44/ 下面的ls-1 | sort-n-k1.8-k1.4-k1 | tail-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

查找是此类任务的常用工具:

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

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

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

你可以删除超过n的文件,比如
find-mtime+3-delete
删除超过3天的文件。我不想在时间上重复44个store\u id not constant文件夹,它有更多storeid,比如45,46,47,87,98..所以我没有constant/my\u dir文件夹。它就像/home/backup/44,43,56,77,---在那个按日期排列的文件夹中,我需要旋转文件夹。@user2481010,这样就可以通过在所有目录中指定路径来使用第一个解决方案,比如
/home/backup/44,43,56,77,
。您还可以在第二个解决方案命令中使用类似于
find/home/backup-type d
的命令来完成您的工作。
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