很多时候备份通常会使用到基于日期来创建文件夹,对于这些日期文件夹下面又有很多子文件夹,对于这些日期文件整个移除,通过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

自动清除日期目录shell脚本的更多相关文章

  1. RHEL自动安装zookeeper的shell脚本

    RHEL自动安装zookeeper的shell脚本 A:本脚本运行的机器,Linux RHEL6 B,C,D,...:待安装zookeeper cluster的机器, Linux RHEL6 首先在脚 ...

  2. Linux自动安装JDK的shell脚本

    Linux自动安装JDK的shell脚本 A:本脚本运行的机器,Linux B:待安装JDK的机器, Linux 首先在脚本运行的机器A上确定可以ssh无密码登录到待安装jdk的机器B上,然后就可以在 ...

  3. zabbix系列(九)zabbix3.0实现自动触发zabbix-agent端shell脚本任务

    zabbix实现自动触发远程脚本执行命令 Zabbix触发器(trigger)达到阀值后会有动作(action)执行:发送告警信息或执行远程命令 环境 Server:基于centos6.5 final ...

  4. 写个自动安装JDK的shell脚本

    #!/bin/bash ################################################# # # INSTALL JDK AUTOMATICALLY # # auth ...

  5. 可以自动安装mysql数据库的一个shell脚本

    发布:thatboy   来源:脚本学堂     [大 中 小] 分享一例shell脚本,可以实现mysql数据库的自动安装,脚本写的不错,无论是用来学习,还是生产环境中应用,都是不错的,有需要的朋友 ...

  6. Linux系统——shell脚本

    shell脚本编程 作用:通过命令行解析的方式,自动执行设定好的程序或命令代码.(若将脚本挂到定时任务中,就会自动在非工作时间里自动触发执行程序) Shell脚本文件以“.sh”结尾 规范的Shell ...

  7. Linux就该这么学--Shell脚本条件语句(一)

    1.条件测试语句能够让Shell脚本根据实际工作灵活调整工作内容,例如判断系统的状态后执行指定的工作,或创建指定数量的用户,批量修改用户密码,这些都可以让Shell脚本通过条件测试语句完成. if条件 ...

  8. CSDN中根据文章自动生成文章目录

    概述 CSDN中有根据文件内容中H标签在文章中自动生成文章目录,看起来比较专业,就想把它搬到自己的博客园中.类似下图 提取JS脚本 通过浏览器开发者工具(IE/Chrome)找到产生文章目录javas ...

  9. shell脚本实例

    备注:一些与传递给shell的参数相关的变量:$# 命令行参数的个数$? 调用命令的返回值$$ 当前进程的进程号$! 最后一个后台命令的进程号$0 命令行的第一个参数,也就是命令名$n 命令行的第n个 ...

随机推荐

  1. 【WIP】Ruby JSON

    创建: 2018/03/22 以后有空补上 注: JSON.generate 参数只能是Obejct或者Array, 不可以是Hash https://docs.ruby-lang.org/ja/la ...

  2. head first python /chapter7 web(python 3 转 python 2.7)

    前言 书中使用的是python3,我这里使用的是python2.7 Web 的目录树 webapp/ ├── cgi-bin │ ├── athletelist.py │ ├── athletemod ...

  3. 题解报告:poj 2503 Babelfish(map)

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  4. ORA-28002错误原因及解决办法

    在oracle database 11g中,默认在default概要文件中设置了“PASSWORD_LIFE_TIME=180天”所导致.密码过期后,业务进程连接数据库异常,影响业务使用.数据库密码过 ...

  5. 事件模型的介绍与Button的ActionListener

    事件监听: 这是个很重要的概念,也是个很重要的模型,vb,vc都是这样用,甚至后面学的web框架也在用.    现在我们可以做很多按钮了吧,但是我们的按钮按它是没反应的,现在我们来看看怎么样才能让它有 ...

  6. Normal equations 正规方程组

    前面我们通过Gradient Descent的方法进行了线性回归,但是梯度下降有如下特点: (1)需要预先选定Learning rate: (2)需要多次iteration: (3)需要Feature ...

  7. python 字符串的split()函数详解(转)

    作者:宋桓公 出处:http://www.cnblogs.com/douzi2/ 如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同 ...

  8. C. Two strings 二分 + 预处理

    http://codeforces.com/contest/762/problem/C 第一个串str[],第二个sub[] 预处理出prefix[i]表示sub的前i位和str[]的最长lcs去到s ...

  9. jdk 1.8下 java ArrayList 添加元素解析

    转载请注明http://www.cnblogs.com/majianming/p/8006452.html 有人问我,java ArrayList底层是怎么实现的?我就回答数组,他再问我,那它是怎么实 ...

  10. [转]windows azure How to use Blob storage from .NET

    本文转自:http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/?rnd=1 ...