셸 스 크 립 트 삭제 N 이전 폴 더 --- 링크 ux 와 mac 의 date 명령 이 다 릅 니 다.
#! /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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JS_3. 내장객체Math는 전역객체이기 때문에 new 키워드 사용하지 않음 new Object() 대신 { } 사용가능 new Array() 대신 [ ] 사용가능 new String()대신 " " 사용가능 new Boolean()대...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.