自动清除日期目录shell脚本

很多时候备份通常会使用到基于日期来创建文件夹,对于这些日期文件夹下面又有很多子文件夹,对于这些日期文件整个移除,通过find结合rm或者delete显得有些力不从心。本文提供一个简单的小脚本,可以嵌入到其他脚本,也可直接调用,如下文供大家参考。

 

1、脚本内容

[python] view plain copy
 
 print?
  1. [root@SZDB ~]# more purge_datedir.sh   
  2. #!/bin/bash  
  3. # Author: Leshami  
  4. # Blog  : http://blog.csdn.net/leshami  
  5.   
  6. RemoveDir=/log/hotbak/physical  
  7. dt=`date +%Y%m%d -d "3 day ago"`  
  8.   
  9. for subdir in `ls $RemoveDir`;  
  10. do  
  11.     if [ "${subdir}" \< "${dt}" ];  
  12.         then   
  13.         rm -rf $RemoveDir/$subdir >/dev/null  
  14.         echo "The directory $RemoveDir/$subdir has been removed."  
  15.     fi  
  16. done  

2、演示

[python] view plain copy
 
 print?
  1. [root@SZDB ~]# ls /log/hotbak/physical  
  2. 20141203  20141210  20141217  20141224  20141231  20150107  20150114  20150125  tmp.sh  
  3. 20141207  20141214  20141221  20141228  20150104  20150111  20150121  20150128  
  4.   
  5. [root@SZDB ~]# ./purge_datedir.sh   
  6. The directory /log/hotbak/physical/20141203 has been removed.  
  7. The directory /log/hotbak/physical/20141207 has been removed.  
  8. The directory /log/hotbak/physical/20141210 has been removed.  
  9. The directory /log/hotbak/physical/20141214 has been removed.  
  10. The directory /log/hotbak/physical/20141217 has been removed.  
  11. The directory /log/hotbak/physical/20141221 has been removed.  
  12. The directory /log/hotbak/physical/20141224 has been removed.  
  13. The directory /log/hotbak/physical/20141228 has been removed.  
  14. The directory /log/hotbak/physical/20141231 has been removed.  
  15. The directory /log/hotbak/physical/20150104 has been removed.  
  16. The directory /log/hotbak/physical/20150107 has been removed.  
  17. The directory /log/hotbak/physical/20150111 has been removed.  
  18. The directory /log/hotbak/physical/20150114 has been removed.  
  19. The directory /log/hotbak/physical/20150121 has been removed.  
  20.   
  21. [root@SZDB ~]# ls /log/hotbak/physical  
  22. 20150125  20150128    
posted @ 2017-10-20 10:39  Yang-Blog  阅读(419)  评论(0编辑  收藏  举报