Background:
The items that are built daily are placed in different folders by date. As today's construction is put into 2015-06-01, tomorrow's will be put into 2015-06-02, and so on. Long time, need a script to delete the folder N days ago. (In this case n=7, the build that was deleted a week ago).
Below the code directly, the Linux version:
#!/bin/bashhistorydir=~/test/today=$ (Date +%y-%m-%d)Echo "---------today is $today-----------"tt= ' Date- DLast-week +%y-%m-%d 'Echo "Next is to delete release before $tt"tt1= ' Date- D $tt+%s '#小于此数值的文件夹删掉#echo $tt 1 forFileinch ${historydir}* Do ifTest- D $file ThenName= ' basename$file`#echo $nameCurr= ' Date- D $name+%s 'if[$curr-le$tt 1] Then Echo "Delete $name-------"Rm-rf${historydir}${name} fi fi Done
Precautions:
1,historydir=~/test/must be followed by a/, otherwise in the later traversal of the folder when for the file in hIsT oR yDIR ?will betheshould beNoon. 2,in thel INux underPasseverT od ay= (date +%y-%m-%d) Gets the date formatted as the 2015-06-01 type, through tt1= ' date-d t t + (date +%s) This is the time stamp, no longer converted, but the date is the default time-of-day hour and minute of the format conversion timestamp.
Not Ps:mac.
3,linux through date-d Last-week +%y-%m-%d to get a week-ago date.
the Ps:mac is not OK.
4, through if test-d $file to determine whether the folder exists,-F is to determine whether the file exists. Name= basename $file
This sentence to get the name of the folder, followed by the name (that is, the date) to the timestamp comparison.
The code on your Mac
#!/bin/bashhistorydir=~/test/today=$ (Date +%y-%m-%d)Echo "---------today is $today-----------"Today1= ' Date-j- F%y-%m-%d$today+%s '#echo "today1= $today 1"#求一周前的时间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 ' #小于此数值的文件夹删掉#echo $tt 1 forFileinch ${historydir}* Do ifTest- D $file ThenName= ' basename$file`Echo $nameCurr= ' Date-j- F%y-%m-%d$name+%s 'if[$curr-le$tt 1] Then Echo "Delete $name"Rm-rf${historydir}${name} fi fi DoneEcho "--------------End---------------"
There are two different places on Linux:
1, when you convert the time of the string to an integer timestamp, on your Mac:
today1= date -j -f %Y-%m-%d $today +%s
2, get 7 days before the date on the Mac to do this:
tt=$ (date-v -7d +%y-%m-%d)
RELATED links:
1,http://willzh.iteye.com/blog/459808
2,http://apple.stackexchange.com/questions/115830/shell-script-for-yesterdays-date
Shell scripts delete folders that are N days ago-----Different from the date command on Linux and Mac