linux自动清理日期目录,自动清除日期目录shell脚本

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

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

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

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值