shell脚本实现仅保留某目录下最新的两个文件

时间:2021-10-14 08:46:59
 1 #!/bin/sh
 2 
 3 export DS_DIR=/home/cxy/test
 4 
 5 if [ ! -d $DS_DIR ]; then
 6     mkdir $DS_DIR
 7 else
 8     echo "$DS_DIR is not existed!"
 9 fi
10 
11 cd $DS_DIR
12 if [ $(ls -l | grep "install-*" | wc -l) > 3 ]; then
13     echo "more than 2 dst_files in $DS_DIR"
14     rm -r $(ls -rt | head -n2)
15 fi