實(shí)現(xiàn)自動(dòng)清除日期目錄shell腳本實(shí)例代碼
實(shí)現(xiàn)自動(dòng)清除日期目錄shell腳本實(shí)例代碼
很多時(shí)候備份通常會(huì)使用到基于日期來創(chuàng)建文件夾,對于這些日期文件夾下面又有很多子文件夾,對于這些日期文件整個(gè)移除,通過find結(jié)合rm或者delete顯得有些力不從心。本文提供一個(gè)簡單的小腳本,可以嵌入到其他腳本,也可直接調(diào)用,如下文供大家參考。
1、腳本內(nèi)容
[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
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Linux在shell中自動(dòng)生成1到100的數(shù)組方法(兩種方法)
之前自己在寫shell腳本的時(shí)候,需要自動(dòng)創(chuàng)建1-100的文本確不知道該如何去創(chuàng)建。今天小編給大家分享兩種方法,需要的朋友參考下2017-02-02創(chuàng)建Linux系統(tǒng)服務(wù)的方法
這篇文章主要介紹了通過linux sh創(chuàng)建Linux系統(tǒng)服務(wù)的方法,需要的朋友可以參考下2014-01-01shell腳本監(jiān)控系統(tǒng)負(fù)載、CPU和內(nèi)存使用情況
這篇文章主要介紹了shell腳本監(jiān)控系統(tǒng)負(fù)載、CPU和內(nèi)存使用情況,本文分別給出監(jiān)控服務(wù)器系統(tǒng)負(fù)載情況、監(jiān)控系統(tǒng)cpu使用情況、、監(jiān)控系統(tǒng)內(nèi)存情況、監(jiān)控系統(tǒng)交換分區(qū)swap使用情況的腳本,需要的朋友可以參考下2014-12-12linux shell中實(shí)現(xiàn)循環(huán)日期的實(shí)例代碼
這篇文章主要介紹了linux shell中實(shí)現(xiàn)循環(huán)日期的實(shí)例代碼,文中還給大家提到了LINUX SHELL遍歷日期(指定輸入兩個(gè)日期)的實(shí)現(xiàn)方法,感興趣的朋友跟隨小編一起看看吧2018-09-09