当前位置: 首页 > 编程笔记 >

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

林德惠
2023-03-14
本文向大家介绍实现自动清除日期目录shell脚本实例代码,包括了实现自动清除日期目录shell脚本实例代码的使用技巧和注意事项,需要的朋友参考一下

实现自动清除日期目录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脚本实现自动发送邮件的例子,包括了Shell脚本实现自动发送邮件的例子的使用技巧和注意事项,需要的朋友参考一下 1、编辑用户Home目录下的.muttrc文件,设置发信环境。 2、发信脚本 3、简要说明:   -s:指定主题 -a:附件 -c:抄送,需要抄送多人的话,须使用多个 -c 选项   使用上面的发信环境可以保证在页面查看邮件的时候,附件名不出现乱码。 其中 .mu

  • 本文向大家介绍shell脚本 自动创建用户详解及实例代码,包括了shell脚本 自动创建用户详解及实例代码的使用技巧和注意事项,需要的朋友参考一下 shell脚本 自动创建用户详解 需求:判断用户zhangsan是否存在,不存在就创建并设置密码为123456 1、vi createuser.sh 2、写入: 3、修改createuser.sh权限 4、执行 感谢阅读,希望能帮助到大家,谢谢大家对本

  • 本文向大家介绍selenium+python实现自动登录脚本,包括了selenium+python实现自动登录脚本的使用技巧和注意事项,需要的朋友参考一下 os:windows 前提:Python,selenium,IEDriverServer.exe,ie浏览器 首先安装Python2.7 安装成功后,计算机联网状态下在cmd命令行下输入:pip install -U selenium sele

  • 本文向大家介绍Shell脚本实现监控kingate并自动启动,包括了Shell脚本实现监控kingate并自动启动的使用技巧和注意事项,需要的朋友参考一下 自己在vps做的kingate服务端,估计最近占用流量太大了,老是被服务商把我vps重启,但kingate这个东西是一旦被强制结束掉,是无法开机启动的,因为要把kingate.pid这个文件删除了才能启动,好吧,对于我们这样的懒人来说,当然不能

  • 本文向大家介绍shell脚本按当前日期输出日志的实现,包括了shell脚本按当前日期输出日志的实现的使用技巧和注意事项,需要的朋友参考一下 shell脚本基本技能是设置变量,输出信息。如下: sh脚本输出到控制台 将以上脚本保存到test.sh脚本文件中,并赋予可执行权限:sudo chmod +x test.sh 执行脚本:bash test.sh 控制台输出: 输出到控制台,并且写入日志文件:

  • 本文向大家介绍Shell脚本实现自动修改IP地址,包括了Shell脚本实现自动修改IP地址的使用技巧和注意事项,需要的朋友参考一下 作为一名Linux SA,日常运维中很多地方都会用到脚本,而服务器的ip一般采用静态ip或者MAC绑定,当然后者比较操作起来相对繁琐,而前者我们可以设置主机名、ip信息、网关等配置。修改成特定的主机名在维护和管理方面也比较方便。如下脚本用途为:修改ip和主机名等相关信