一个自动清除日期目录的shell脚本,自动清理指定日期格式的linux目录,删除基于日期来创建的文件夹。 在linux系统中,有很多基于日期来创建文件夹,甚至还会有层级很深的子文件夹,对于这些日期文件整个移除,通过find结合rm或delete删除,费时费力。
使用以下shell脚本,可以嵌入到其他脚本,也可直接调用,用于清除日期目录。
[root@itnetcn ~]# 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@itnetcn ~]# ls /log/hotbak/physical
20141203 20141210 20141217 20141224 20141231 20150107 20150114 20150125 tmp.sh 20141207 20141214 20141221 20141228 20150104 20150111 20150121 20150128 [root@itnetcn ~]# ./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@itnetcn ~]# ls /log/hotbak/physical 20150125 20150128 (责任编辑:IT) |