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

1、脚本内容

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

2、演示

  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. [root@SZDB ~]# ./purge_datedir.sh
  5. The directory /log/hotbak/physical/20141203 has been removed.
  6. The directory /log/hotbak/physical/20141207 has been removed.
  7. The directory /log/hotbak/physical/20141210 has been removed.
  8. The directory /log/hotbak/physical/20141214 has been removed.
  9. The directory /log/hotbak/physical/20141217 has been removed.
  10. The directory /log/hotbak/physical/20141221 has been removed.
  11. The directory /log/hotbak/physical/20141224 has been removed.
  12. The directory /log/hotbak/physical/20141228 has been removed.
  13. The directory /log/hotbak/physical/20141231 has been removed.
  14. The directory /log/hotbak/physical/20150104 has been removed.
  15. The directory /log/hotbak/physical/20150107 has been removed.
  16. The directory /log/hotbak/physical/20150111 has been removed.
  17. The directory /log/hotbak/physical/20150114 has been removed.
  18. The directory /log/hotbak/physical/20150121 has been removed.
  19. [root@SZDB ~]# ls /log/hotbak/physical
  20. 20150125  20150128

最新文章

  1. Mysql - 函数
  2. python3 安装scrapy
  3. Google软件构建工具Bazel FAQ
  4. S7-200系列PLC与WINCC以太网通信CP243i的实例
  5. JSOI地铁换票 (贪心)
  6. linux管道学习(二)
  7. MSSQL 常用内置函数
  8. 7. Reverse Integer
  9. myeclipse10.7安装git插件
  10. (转ORCLE导入导出命令)
  11. EFI系统引导的一些零碎知识点
  12. thinkphp中上传图片以及制成缩略图
  13. 移动端 去除onclick点击事件出现的背景色框
  14. num2cell
  15. 201621123002《JAVA程序设计》第五周学习总结
  16. openresty 集成 keycloak-oauth-oidc
  17. Python之路 - 网络编程之粘包
  18. apiCloud上传头像
  19. ios学习路线—Objective-C(Runtime消息机制)
  20. spring注解-@Autowired。@Resource。@Service

热门文章

  1. [App Store Connect帮助]八、维护您的 App(2)将 App 从 App Store 中移除
  2. Ocelot(六)- 架构图
  3. 又一篇Centos7下的asp.net core部署教程
  4. 拓扑排序/DFS HDOJ 4324 Triangle LOVE
  5. 贪心 Codeforces Round #109 (Div. 2) B. Combination
  6. B - Archer
  7. 187 Repeated DNA Sequences 重复的DNA序列
  8. 【开源】基于EF6+MVC5+API2+Easyui1.4.5+Easyui管理模板开发的管理系统
  9. AJPFX关于多态的应用
  10. vue项目开发前的es6的知识储备