[root@scm-dzm testfolder]# ll
total 4 drwxr-xr-x. 2 root root 6 Jul 14 00:53 20160722 drwxr-xr-x. 2 root root 6 Jul 14 00:53 20160723 drwxr-xr-x. 2 root root 6 Jul 14 00:53 20160724 drwxr-xr-x. 2 root root 6 Jul 14 00:45 20160725 drwxr-xr-x. 2 root root 6 Jul 14 00:45 20160726 drwxr-xr-x. 2 root root 6 Jul 14 00:45 20160727 -rwxr-xr-x. 1 root root 470 Jul 14 00:44 delfolder.sh
[root@scm-dzm testfolder]# cat delfolder.sh
#!/bin/bash # rm the backup files on 3days ago backupfolder=/root/testfolder/ cd $backupfolder statistics() { folderlist=`ls $backupfolder| tr ' ' '\n'|egrep '[0-9]{8}' |sort -n` backupnum=`echo $folderlist | tr ' ' '\n'| wc -l ` echo " backupnum = $backupnum" } statistics while [ $backupnum -gt 3 ] do dellist=${folderlist:0:9} echo "dellist = $dellist" rm -rf $backupfolder$dellist statistics done
[root@scm-dzm testfolder]# ./delfolder.sh
backupnum = 6 dellist = 20160722 20160722 backupnum = 5 dellist = 20160723 20160723 backupnum = 4 dellist = 20160724 20160724 backupnum = 3
[root@scm-dzm testfolder]# ll
total 4 drwxr-xr-x. 2 root root 6 Jul 14 00:45 20160725 drwxr-xr-x. 2 root root 6 Jul 14 00:45 20160726 drwxr-xr-x. 2 root root 6 Jul 14 00:45 20160727 -rwxr-xr-x. 1 root root 470 Jul 14 00:44 delfolder.sh
原创文章,作者:Net20_天意,如若转载,请注明出处:http://www.178linux.com/24151