加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2023-10-03 10:55:22 所属栏目:PHP教程 来源:
导读:实现自动清除日期目录shell脚本实例代码

很多时候备份通常会使用到基于日期来创建文件夹,对于这些日期文件夹下面又有很多子文件夹,对于这些日期文件整个移除,通过find结合rm或者delete显得有些力不从心。本文提

实现自动清除日期目录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"`  
    
forsubdirin`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  
2014120320141210201412172014122420141231201501072015011420150125tmp.sh  
2014120720141214201412212014122820150104201501112015012120150128  
    
[root@SZDB ~]# ./purge_datedir.sh   
The directory/log/hotbak/physical/20141203has been removed.  
The directory/log/hotbak/physical/20141207has been removed.  
The directory/log/hotbak/physical/20141210has been removed.  
The directory/log/hotbak/physical/20141214has been removed.  
The directory/log/hotbak/physical/20141217has been removed.  
The directory/log/hotbak/physical/20141221has been removed.  
The directory/log/hotbak/physical/20141224has been removed.  
The directory/log/hotbak/physical/20141228has been removed.  
The directory/log/hotbak/physical/20141231has been removed.  
The directory/log/hotbak/physical/20150104has been removed.  
The directory/log/hotbak/physical/20150107has been removed.  
The directory/log/hotbak/physical/20150111has been removed.  
The directory/log/hotbak/physical/20150114has been removed.  
The directory/log/hotbak/physical/20150121has been removed.  
    
[root@SZDB ~]# ls /log/hotbak/physical  
2015012520150128 

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章