自动清除日期目录shell脚本

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

 

1、脚本内容

[root@SZDB ~]# more purge_datedir.sh
#!/bin/bash
# Author: Leshami
# Blog  : http://blog.csdn.net/leshami

RemoveDir=/log/hotbak/physical
dt=`date +%Y%m%d -d "3 day ago"`

for subdir in `ls $RemoveDir`;
do
    if [ "${subdir}" \< "${dt}" ];
        then
        rm -rf $RemoveDir/$subdir >/dev/null
        echo "The directory $RemoveDir/$subdir has been removed."
    fi
done

2、演示

[root@SZDB ~]# ls /log/hotbak/physical
20141203  20141210  20141217  20141224  20141231  20150107  20150114  20150125  tmp.sh
20141207  20141214  20141221  20141228  20150104  20150111  20150121  20150128

[root@SZDB ~]# ./purge_datedir.sh
The directory /log/hotbak/physical/20141203 has been removed.
The directory /log/hotbak/physical/20141207 has been removed.
The directory /log/hotbak/physical/20141210 has been removed.
The directory /log/hotbak/physical/20141214 has been removed.
The directory /log/hotbak/physical/20141217 has been removed.
The directory /log/hotbak/physical/20141221 has been removed.
The directory /log/hotbak/physical/20141224 has been removed.
The directory /log/hotbak/physical/20141228 has been removed.
The directory /log/hotbak/physical/20141231 has been removed.
The directory /log/hotbak/physical/20150104 has been removed.
The directory /log/hotbak/physical/20150107 has been removed.
The directory /log/hotbak/physical/20150111 has been removed.
The directory /log/hotbak/physical/20150114 has been removed.
The directory /log/hotbak/physical/20150121 has been removed.

[root@SZDB ~]# ls /log/hotbak/physical
20150125  20150128
时间: 2024-10-12 14:19:24

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

shell 自动重启nginx php shell脚本

shell 自动重启nginx php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

自动创建分区的shell脚本

题目:用shell脚本自动添加分区,已实现功能如下: 1.检查扩展分区是否,且是否可以添加,如果可以则把所有的剩余空间设置为扩展分区 2.当扩展分区存在,则输入要添加的分区大小,仅接受大于1的正整数 代码如下: #!/bin/bash Hard='/dev/sda' Exten=`fdisk -l $Hard|grep Extended` Maxnum=`fdisk -l $Hard|grep -o "^/dev/sda[1-9]\>"|tr -d [[:punct:]]|tr

下载Google官方/CM Android源代码自动重新开始的Shell脚本

        国内由于某种原因,下载CM或Google官方的Android源代码总容易中断.总看着机器,一中断就重新执行repo sync还太麻烦,所以我特意编写了一段shell脚本(download.sh).通过获取shell最后返回的状态码来决定是否再次执行repo sync命令. #!/bin/bash echo "======start repo sync======" repo sync # 第一次下载android源代码 while [ $? != 0 ]; do ech

自动重启服务的shell脚本代码_linux shell

复制代码 代码如下: #!/bin/bash if [ ! -f /tmp/down_count ];then echo "0" > /tmp/down_count fi curl -I tomcat-host -o "/tmp/status" >/dev/null 2>&1 code=`awk 'NR==1 {print $2}' /tmp/status` if [ "$[code]" -ge 500 ];then d

监控php-fpm并自动重启服务的shell脚本_linux shell

脚本代码: 复制代码 代码如下: #!/bin/bash #变量初始化process="php-fpm" #进程名startCmd="/etc/init.d/php-fpm start" #启动命令down=0 while truedo    #取得http状态码    code=$(curl -H "Host:www.jb51.net" -m 5 -L -s -w %{http_code} http://127.0.0.1 -o /dev/nu

Centos7系下自动修改网卡名称shell脚本

直接上代码! #!/bin/sh   [ -f /etc/init.d/functions ] && . /etc/init.d/functions ###修改网卡配置文件名称 function net () { b0=-1 cat /proc/net/dev |grep ':' | grep -v 'lo' | cut -d: -f1 | sort >> /tmp/net_name.txt do c0=$line b0=`expr $b0 + 1` mv /etc/sysco

一个监控Squid运行进程数并自动重启的简洁Shell脚本分享_linux shell

复制代码 代码如下: #!/bin/sh while sleep 30 do SquidNum=`ps -ef|grep squid|grep -v grep|wc -l` #HttpNum=`netstat -an|grep 0.0.0.0:80|grep -v grep|wc -l` #80状态 if [ $SquidNum != 3 ] ; then /etc/rc3.duid start adddate=`date +%Y-%m-%d`" "`date +%H:%M:%S` e

Linux/Unix shell 脚本清除归档日志文件

      对于DEV以及UAT环境,有些时候,数据库需要处于归档模式,但并不需要备份数据库.因此,archive归档日志不停的增长导致磁盘空间被大量耗用.对于这种情形,可以使用一个shell脚本来定时自动清除这些归档日志.本文给出了清除归档日志的脚本.   1.清除归档日志shell脚本   robin@SZDB:~/dba_scripts/custom/bin> more remove_arch_dump.sh #!/bin/bash # --------------------------

Shell脚本定时备份清除运行系统日志的代码_linux shell

一.写备份并清除老日志Shell脚本: 复制代码 代码如下: #!/bin/sh#backup eoslog#author rhao#date 2008-12-27 #定义环境变量EOS_HOME=/home/eosSAS_HOME=/home/eos/SAS # 测试主辅结点if test -d '/home/eos/SAS/SAS1_web1'then        SAS_LOG_HOME=$SAS_HOME/SAS1_web1/SAS_Domain/binelse        SAS_