实例男篮世界杯赌球::实现自动清除日期目录

实现自动清除日期目录shell脚本实例代码

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

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

1、脚本内容

1、脚本内容

[root@SZDB ~]# more purge_datedir.sh
#!/bin/bash
# Author: Leshami
# Blog  : http://www.linuxidc.com

 

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

[root@SZDB ~]# more purge_datedir.sh  

2、演示

#!/bin/bash 

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

# Author: Leshami 

[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.

# Blog : http://blog.csdn.net/leshami 

[root@SZDB ~]# ls /log/hotbak/physical
20150125  20150128 

   

cat命令利用Linux重定向合并文件 http://www.linuxidc.com/Linux/2015-01/112122.htm

RemoveDir=/log/hotbak/physical 

Shell编程浅析 http://www.linuxidc.com/Linux/2014-08/105379.htm 

dt=`date %Y%m%d-d"3 day ago"` 

Linux Shell参数替换 http://www.linuxidc.com/Linux/2013-06/85356.htm

   

Shell for参数 http://www.linuxidc.com/Linux/2013-07/87335.htm

forsubdirin`ls $RemoveDir`; 

Linux/Unix Shell 参数传递到SQL脚本 http://www.linuxidc.com/Linux/2013-03/80568.htm

do 

Shell脚本中参数传递方法介绍 http://www.linuxidc.com/Linux/2012-08/69155.htm

  if["${subdir}"<"${dt}"]; 

Shell脚本传递命令行参数 http://www.linuxidc.com/Linux/2012-01/52192.htm

    then  

Linux Shell 通配符、转义字符、元字符、特殊字符 http://www.linuxidc.com/Linux/2014-10/108111.htm

    rm-rf $RemoveDir/$subdir >/dev/null 

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-01/112500.htm

    echo"The directory $RemoveDir/$subdir has been removed." 

男篮世界杯赌球 1

  fi 

done

 

2、演示

 

本文由美洲杯赌球发布于计算机教程,转载请注明出处:实例男篮世界杯赌球::实现自动清除日期目录

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。