實(shí)現(xiàn)自動(dòng)清除日期目錄shell腳本實(shí)例代碼
實(shí)現(xiàn)自動(dòng)清除日期目錄shell腳本實(shí)例代碼
很多時(shí)候備份通常會(huì)使用到基于日期來(lái)創(chuàng)建文件夾,對(duì)于這些日期文件夾下面又有很多子文件夾,對(duì)于這些日期文件整個(gè)移除,通過(guò)find結(jié)合rm或者delete顯得有些力不從心。本文提供一個(gè)簡(jiǎn)單的小腳本,可以嵌入到其他腳本,也可直接調(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
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
shell查找某字符串在某文件中出現(xiàn)行數(shù)的方法
這篇文章主要介紹了shell查找某字符串在某文件中出現(xiàn)行數(shù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Shell腳本字符串單引號(hào)和雙引號(hào)的區(qū)別淺析
這篇文章主要介紹了Shell腳本字符串單引號(hào)和雙引號(hào)的區(qū)別淺析,,并羅列了一些字符串的操作方法,需要的朋友可以參考下2014-07-07
Shell使用Epoch進(jìn)行日期時(shí)間轉(zhuǎn)換和計(jì)算的幾個(gè)小函數(shù)
這篇文章主要介紹了當(dāng)你遇到一個(gè)date命令不給力的系統(tǒng)時(shí),可以試試這幾個(gè)小函數(shù),需要的朋友可以參考下2016-12-12
Linux shell下30個(gè)有趣的命令和提示(推薦)
這些是我收集了多年的Linux shell的30個(gè)有趣的命令和提示。特此分享到腳本之家平臺(tái),供大家參考2017-10-10
shell腳本實(shí)現(xiàn)ssh自動(dòng)登錄功能分享
mac下沒有找到好用的類似secureCRT,就自己寫了個(gè)自動(dòng)登錄的腳本,分享一下,如果是新浪的,就基本不用修改代碼就直接能用2014-04-04
一天一個(gè)shell命令 linux文件內(nèi)容操作系列-cat命令詳解
這篇文章主要介紹了一天一個(gè)shell命令 linux文件內(nèi)容操作系列-cat命令詳解,需要的朋友可以參考下2016-06-06
3000字掃盲shell基礎(chǔ)知識(shí)(新手必備)
這篇文章主要介紹了3000字掃盲shell基礎(chǔ)知識(shí),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2020-06-06

