实现自动清除日期目录shell脚本实例代码
实现自动清除日期目录shell脚本实例代码
很多时候备份通常会使用到基于日期来创建文件夹,对于这些日期文件夹下面又有很多子文件夹,对于这些日期文件整个移除,通过find结合rm或者delete显得有些力不从心。本文提供一个简单的小脚本,可以嵌入到其他脚本,也可直接调用,如下文供大家参考。
1、脚本内容
[root@SZDB~]#morepurge_datedir.sh
#!/bin/bash
#Author:Leshami
#Blog:http://blog.csdn.net/leshami
RemoveDir=/log/hotbak/physical
dt=`date+%Y%m%d-d"3dayago"`
forsubdirin`ls$RemoveDir`;
do
if["${subdir}"\<"${dt}"];
then
rm-rf$RemoveDir/$subdir>/dev/null
echo"Thedirectory$RemoveDir/$subdirhasbeenremoved."
fi
done
2、演示
[root@SZDB~]#ls/log/hotbak/physical 2014120320141210201412172014122420141231201501072015011420150125tmp.sh 2014120720141214201412212014122820150104201501112015012120150128 [root@SZDB~]#./purge_datedir.sh Thedirectory/log/hotbak/physical/20141203hasbeenremoved. Thedirectory/log/hotbak/physical/20141207hasbeenremoved. Thedirectory/log/hotbak/physical/20141210hasbeenremoved. Thedirectory/log/hotbak/physical/20141214hasbeenremoved. Thedirectory/log/hotbak/physical/20141217hasbeenremoved. Thedirectory/log/hotbak/physical/20141221hasbeenremoved. Thedirectory/log/hotbak/physical/20141224hasbeenremoved. Thedirectory/log/hotbak/physical/20141228hasbeenremoved. Thedirectory/log/hotbak/physical/20141231hasbeenremoved. Thedirectory/log/hotbak/physical/20150104hasbeenremoved. Thedirectory/log/hotbak/physical/20150107hasbeenremoved. Thedirectory/log/hotbak/physical/20150111hasbeenremoved. Thedirectory/log/hotbak/physical/20150114hasbeenremoved. Thedirectory/log/hotbak/physical/20150121hasbeenremoved. [root@SZDB~]#ls/log/hotbak/physical 2015012520150128
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短