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

所属分类: 脚本专栏 / linux shell 阅读数: 1293
收藏 0 赞 0 分享

实现自动清除日期目录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  

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

Shell脚本中计算字符串长度的5种方法

这篇文章主要介绍了Shell脚本中计算字符串长度的5种方法,来自于个人Shell脚本长期的开发经验,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本实现把进程负载均衡到多核CPU中

这篇文章主要介绍了Shell脚本实现把进程负载均衡到多核CPU中,可以把进程指定运行在某个CPU中,需要的朋友可以参考下
收藏 0 赞 0 分享

5个Shell脚本编程入门练习例子

这篇文章主要介绍了5个Shell脚本编程入门例子,涵盖了各种操作,又有一些游戏的性质,作为入门练习例子是不很不错的,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本编程中常用的数学运算实例

这篇文章主要介绍了Shell脚本编程中常用的数学运算实例,包含最基本的加减乘除,还有质数、偶数的判断等,需要的朋友可以参考下
收藏 0 赞 0 分享

5个实用的shell脚本面试题和答案

这篇文章主要介绍了5个实用的shell脚本面试题和答案,给出的脚本堪称编码规范,麻雀虽小,异常处理,友好提示,一应俱全,值得学习,需要的朋友可以参考下
收藏 0 赞 0 分享

使用bash shell删除目录中的特定文件的3种方法

这篇文章主要介绍了使用bash shell删除目录中的特定文件的3种方法,分别为扩展模式匹配符、GLOBIGNORE 变量和find 命令,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell获取文件的文件名和扩展名的例子

这篇文章主要介绍了Shell获取文件的文件名和扩展名的例子,简明版的代码实例,看了就懂,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell多线程操作及线程数控制实例

这篇文章主要介绍了Shell多线程操作及线程数控制实例,文中从单线程实现一个需求开始,不断加入代码实现多线程以及线程数的控制功能,需要的朋友可以参考下
收藏 0 赞 0 分享

阿里云主机一键安装lamp、lnmp环境的shell脚本分享

这篇文章主要介绍了阿里云主机一键安装lamp、lnmp环境的shell脚本分享,需要的朋友可以参考下
收藏 0 赞 0 分享

shell脚本转发80端口数据包给Node.js服务器

开发基于Node.js的WEB应用很方便,但是服务端口问题很蛋疼,由于Linux内核规定普通用户只能使用大于1024的端口号,所以使用普通用户运行Node.js服务就不能监听80端口
收藏 0 赞 0 分享
查看更多