shell脚本删除N天前的目录-----附linux和mac上date命令的不同
2017-07-31 14:06
471 查看
背景:
每日构建的东西。按日期放到不同的目录里。
现在天的构建放到2015-06-01里,明天的就放到2015-06-02里,依次类推。时间久了。须要一个脚本删除N天前的目录。(本例中N=7。即删除一周前的构建)。
以下直接上代码,linux版:
注意事项:
1,historyDir=~/test/后面一定要带/,否则在后面的遍历目录时for file in ${historyDir}*会相应不上。
2,在linux下通过today=$(date +%Y-%m-%d)获得格式为2015-06-01类型的日期,通过
得到整形的时间戳。
当然也能够在获得时间的时候就用$(date +%s)这样直接得到的就是时间戳,不用再转换了,可是日期是默认的年月日小时分秒的格式转换的时间戳。
PS:MAC下不行。
3,linux里通过date -d last-week +%Y-%m-%d来获得一周前的日期。
PS:MAC下没行。
4,通过 if test -d $file来推断目录是否存在。-f是推断文件是否存在。
这句话获得目录的名字,之后是将名字(也就是日期)转为时间戳比較。
跟linux上不同之处有二:
1。将字符串的时间转为整数的时间戳时,mac上要这样:
2。获得7天之前的日期mac上要这样:
相关链接:
1,http://willzh.iteye.com/blog/459808
2,http://apple.stackexchange.com/questions/115830/shell-script-for-yesterdays-date
每日构建的东西。按日期放到不同的目录里。
现在天的构建放到2015-06-01里,明天的就放到2015-06-02里,依次类推。时间久了。须要一个脚本删除N天前的目录。(本例中N=7。即删除一周前的构建)。
以下直接上代码,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` #小于此数值的目录删掉 #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,linux里通过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---------------"
跟linux上不同之处有二:
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
相关文章推荐
- shell脚本删除N天前的文件夹-----附linux和mac上date命令的不同
- Linux系统的rm命令太危险,一不小心就会删除系统文件。写一个shell脚本来替换系统的rm命令,
- Linux或者MAC下查找指定目录下的所有指定名字的文件并删除的命令
- Linux监控系统磁盘使用比例,当使用率达到指定比例,删除指定目录下的所有文件的shell脚本
- Linux下使用Shell脚本删除一个目录下的所有子目录和文件
- centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课
- linux-shell命令之rm(remove)【删除文件或目录】
- 删除n天前的所有目录和文件 - linux命令
- 如何用shell脚本删除目录结构为日期的目录(date)
- linux下遍历目录和文件,删除过期天数目录或文件shell脚本
- linux-shell命令之rmdir(remove dir)【删除目录】
- ubuntu linux shell下删除目录命令
- 两次删除我linux系统的shell脚本!!!!!sudo rm -rf 命令慎用啊!!!
- linux学习第六十三篇:Shell脚本介绍,Shell脚本结构和执行,date命令用法,Shell脚本中的变量
- [Linux]定时执行命令的shell脚本,删除脚本自身,判断输出是否为空
- linux的shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量
- Linux下shell脚本编写:常用命令
- 使用shell脚本删除当前目录下.c文件对应的可执行文件
- AIX上的date命令和循环语句与linux上的不同
- 20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量