셸 스 크 립 트 삭제 N 이전 폴 더 --- 링크 ux 와 mac 의 date 명령 이 다 릅 니 다.

배경: 매일 구 축 된 물건 은 날짜 에 따라 다른 폴 더 에 넣 습 니 다.오늘 의 구축 은 2015 - 06 - 01 리 에 놓 고 내일 의 것 은 2015 - 06 - 02 리 에 놓 고 순서대로 유추 합 니 다.시간 이 지나 면 N 이전 폴 더 를 삭제 하 는 스 크 립 트 가 필요 합 니 다.(이 예 에서 N = 7, 즉 일주일 전 빌 드 를 삭제 합 니 다).다음은 코드 를 직접 올 립 니 다. Liux 버 전:
#! /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`  #           
#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

주의사항: 1, historyDir = ~/test/뒤에 있 는 폴 더 를 옮 겨 다 닐 때 for file in ${historyDir} * 이 대응 하지 않 습 니 다.
2, linux 에서 today = $(date +% Y -% m -% d) 를 통 해 2015 - 06 - 01 형식의 날 짜 를 얻 습 니 다.
tt1=`date -d $tt +%s`

성형 타임 스탬프 를 받다.물론 시간 을 얻 을 때 $(date +% s) 로 직접 얻 을 수 있 는 것 은 시간 스탬프 입 니 다. 더 이상 바 꾸 지 않 아 도 됩 니 다. 하지만 날 짜 는 기본 적 인 년 월 일 시간 분 초 형식 으로 바 뀌 는 시간 스탬프 입 니 다.PS: MAC 에 서 는 안 돼 요.
3. Liux 에 서 는 date - d last - week +% Y -% m -% d 를 통 해 일주일 전의 날 짜 를 얻 을 수 있 습 니 다.PS: MAC 에 서 는 안 돼 요.4. if test - d $file 을 통 해 폴 더 가 존재 하 는 지 여 부 를 판단 합 니 다. - f 는 파일 이 존재 하 는 지 여 부 를 판단 합 니 다.
name=`basename $file`

이 말 은 폴 더 의 이름 을 얻 은 다음 이름 (즉 날짜) 을 타임 스탬프 로 비교 합 니 다.
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"

#       
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 $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---------------"

Liux 와 다른 점 은 2: 1 입 니 다. 문자열 의 시간 을 정수 로 바 꿀 때 mac 에 서 는 이렇게 해 야 합 니 다.
today1=`date -j -f %Y-%m-%d $today +%s`

2, 7 일 전 날 짜 를 얻 으 려 면 mac 에서 이렇게 해 야 합 니 다:
tt=$(date -v -7d +%Y-%m-%d)

관련 링크:
1,http://willzh.iteye.com/blog/459808 2,http://apple.stackexchange.com/questions/115830/shell-script-for-yesterdays-date

좋은 웹페이지 즐겨찾기