前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自动清除日期目录shell脚本

自动清除日期目录shell脚本

作者头像
Leshami
发布2018-08-13 14:40:46
5920
发布2018-08-13 14:40:46
举报

    很多时候备份通常会使用到基于日期来创建文件夹,对于这些日期文件夹下面又有很多子文件夹,对于这些日期文件整个移除,通过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  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年01月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档