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

所属分类: 脚本专栏 / linux shell 阅读数: 1357
收藏 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  

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

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

linux sudo密码输入时显示星号的操作方法

在输入linux sudo密码的时候终端是不显示任何东西的,我们不知道我们按下的密码到底输入进去了没有,如果可以像在网站上登录账号密码的时候显示星号多好,下面小编给大家介绍下linux sudo密码输入时显示星号的操作方法
收藏 0 赞 0 分享

如何让history命令显示日期和时间

我们都对 history 命令很熟悉。它将终端上 bash 执行过的所有命令存储到 .bash_history 文件中,来帮助我们复查用户之前执行过的命令。这篇文章主要介绍了让history命令显示日期和时间的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

shell实现图书管理系统

这篇文章主要介绍了shell实现图书管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

shell实现学生成绩管理系统

这篇文章主要为大家详细介绍了shell实现学生成绩管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

shell编程实现学生成绩管理系统

这篇文章主要为大家详细介绍了shell编程实现学生成绩管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

利用Linux中的crontab实现分布式项目定时任务功能

crond是Linux用来定期执行程序的命令.这篇文章主要介绍了利用Linux中的crontab实现分布式项目定时任务,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux之时钟中断详解

这篇文章主要介绍了Linux之时钟中断详解,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

linux系统上传下载命令rz和sz的教程

这篇文章主要介绍了linux系统上传下载命令rz和sz的安装测试方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell编程之特殊变量的笔记小结

这篇文章主要介绍了Shell编程之特殊变量的笔记小结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解Shell编程之变量数值计算(一)

本篇文章主要介绍了详解Shell编程之变量数值计算,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多