自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

shell腳本刪除N天前的文件夾

網(wǎng)絡(luò) 網(wǎng)絡(luò)管理 網(wǎng)絡(luò)運(yùn)維
你知道怎么用shell腳本刪除N天前的文件夾嗎?本文為您具體講述,希望能對(duì)您有所幫助。

背景:

每日構(gòu)建的東西,按日期放到不同的文件夾里。如今天的構(gòu)建放到2015-06-01里,明天的就放到2015-06-02里,依次類推。時(shí)間久了,需要一個(gè)腳本刪除N天前的文件夾。(本例中N=7,即刪除一周前的構(gòu)建)。

下面直接上代碼,linux版:

#! /bin/bash

historyDir=~/test/

today=$(date +%Y-%m-%d)

echo "---------today is $today-----------"

tt=`date -d last-week +%Y-%m-%d`

echo "next is to delete release before $tt"

tt1=`date -d $tt +%s` #小于此數(shù)值的文件夾刪掉

#echo $tt1

for file in ${historyDir}*

do

if test -d $file

then

name=`basename $file`

#echo $name

curr=`date -d $name +%s`

if [ $curr -le $tt1 ]

then

echo " delete $name-------"

rm -rf ${historyDir}${name}

fi

fi

done

注意事項(xiàng):

1,historyDir=~/test/后面一定要帶/,否則在后面的遍歷文件夾時(shí)for file in ${historyDir}*會(huì)對(duì)應(yīng)不上。

2,在linux下通過today=$(date +%Y-%m-%d)獲得格式為2015-06-01類型的日期,通過

tt1=`date -d $tt +%s`

得到整形的時(shí)間戳。當(dāng)然也可以在獲得時(shí)間的時(shí)候就用$(date +%s)這樣直接得到的就是時(shí)間戳,不用再轉(zhuǎn)換了,但是日期是默認(rèn)的年月日小時(shí)分秒的格式轉(zhuǎn)換的時(shí)間戳。

PS:MAC下不行。

3,linux里通過date -d last-week +%Y-%m-%d來獲得一周前的日期。

PS:MAC下沒行。

4,通過 if test -d $file來判斷文件夾是否存在,-f是判斷文件是否存在。

name=`basename $file`

這句話獲得文件夾的名字,之后是將名字(也就是日期)轉(zhuǎn)為時(shí)間戳比較。

MAC上的代碼

#! /bin/bash

historyDir=~/test/

today=$(date +%Y-%m-%d)

echo "---------today is $today-----------"

today1=`date -j -f %Y-%m-%d $today +%s`

#echo "today1=$today1"

#求一周前的時(shí)間

tt=$(date -v -7d +%Y-%m-%d)

echo "next is to delete release before $tt"

tt1=`date -j -f %Y-%m-%d $tt +%s` #linux上可以這樣`date -d $tt +%s` #小于此數(shù)值的文件夾刪掉

#echo $tt1

for file in ${historyDir}*

do

if test -d $file

then

name=`basename $file`

echo $name

curr=`date -j -f %Y-%m-%d $name +%s`

if [ $curr -le $tt1 ]

then

echo " delete $name"

rm -rf ${historyDir}${name}

fi

fi

done

echo "--------------end---------------"

跟linux上不同之處有二:

1,將字符串的時(shí)間轉(zhuǎn)為整數(shù)的時(shí)間戳?xí)r,mac上要這樣:

today1=`date -j -f %Y-%m-%d $today +%s`

2,獲得7天之前的日期mac上要這樣:

tt=$(date -v -7d +%Y-%m-%d)
責(zé)任編輯:何妍 來源: CSDN博客
相關(guān)推薦

2019-10-22 17:33:57

LinuxBash腳本

2014-02-20 13:01:23

Linux目錄文件

2013-05-28 10:17:02

Windows.old故障恢復(fù)

2013-09-24 10:20:43

技術(shù)刪除Windows.old

2012-07-27 16:46:17

Windows 7 操作系統(tǒng)

2023-05-13 17:43:17

Linux文件文件夾

2021-08-16 13:34:07

Linux終端刪除文件

2010-01-21 13:34:56

VB.NET刪除文件夾

2009-10-27 17:59:16

VB.NET刪除文件夾

2009-08-17 07:55:00

C#文件操作

2025-04-16 10:56:44

2009-12-03 10:18:32

Linux文件夾執(zhí)行權(quán)限

2011-08-04 15:36:32

文件夾病毒

2021-09-14 14:02:40

手機(jī)內(nèi)存技術(shù)

2020-09-23 08:53:48

父文件夾模塊Python

2010-12-31 13:35:25

文件夾重定向

2021-04-14 10:25:19

電腦磁盤微軟

2021-05-14 23:29:01

Windows10微軟軟件

2023-09-02 20:10:06

Ubuntu文件夾圖標(biāo)

2024-12-06 15:11:34

Python文件夾目錄
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)