请注意,我不能在目标环境中使用“查找”
我需要在linux shell脚本中删除超过7天的所有文件。 就像是:
FILES=./path/to/dir for f in $FILES do echo "Processing $f file..." # take action on each file. $f store current file name # perhaps stat each file to get the last modified date and then delete files with date older than today -7 days. done
我可以使用“统计”来做到这一点? 我正在尝试使用
find *.gz -mtime +7 -delete
但发现我不能在目标系统上使用find(没有cron用户的权限,这是不能改变的)。 目标系统是Redhat Enterprise。
如何以编程方式检测Linux或OS X中的可用实用程序
使用Sed来replace旧的时间戳与当前的
在ShellScript中,基于Curl输出分配variables
有没有办法在手册页中寻找一个标志?
文件名格式如下:
gzip> / mnt / target03 / rest-of-path / web / backups / DATABASENAME_ date "+%Y-%m-%d" .gz
我如何设置Git命令的自动完成?
“参数列表太长”限制是否适用于shell buildins?
variables作为bash脚本中的命令
符号链接文件比X年龄更新,然后稍后删除符号链接一次文件年龄?
既然你有时间的文件名,然后用它来定时删除heres一些代码,这样做:
这个脚本获得自纪元以来的当前时间,然后计算7天前的时间戳。 然后为每个文件分析文件名并将嵌入在每个文件名中的日期转换为时间戳,然后比较时间戳以确定要删除的文件。 使用时间戳可以直接处理日期(闰年,几个月内的不同日期等)
#funciton to get timestamp X days prior to input timestamp # arg1 = number of days past input timestamp # arg2 = timestamp ( eg 1324505111 ) seconds past epoch getTimestampDaysInPast () { daysinpast=$1 seconds=$2 while [ $daysinpast -gt 0 ] ; do daysinpast=`expr $daysinpast - 1` seconds=`expr $seconds - 86400` done # make midnight mod=`expr $seconds % 86400` seconds=`expr $seconds - $mod` echo $seconds } # get current time in seconds since epoch getCurrentTime() { echo `date +"%s"` } # parse format and convert time to timestamp # eg 2011-12-23 -> 1324505111 # arg1 = filename with date string in format %Y-%m-%d getFileTimestamp () { filename=$1 date=`echo $filename | sed "s/[^0-9-]*([0-9-]*).*/1/g"` ts=`date -d $date | date +"%s"` echo $ts } ########################### MAIN ############################ # Expect directory where files are to be deleted to be first # arg on commandline. If not provided then use current working # directory FILEDIR=`pwd` if [ $# -gt 0 ] ; then FILEDIR=$1 fi cd $FILEDIR Now=`getCurrentTime` mustBeBefore=`getTimestampDaysInPast 7 $Now` SAVEIFS=$IFS # need this to loop around spaces with filenames IFS=$(echo -en "nb") # for safety change this glob to something more restrictive for f in * ; do filetime=`getFileTimestamp $f` echo "$filetime lt $mustBeBefore" if [ $filetime -lt $mustBeBefore ] ; then # uncomment this when you have tested this on your system echo "rm -f $f" fi done # only need this if you are going to be doing something else IFS=$SAVEIFS
这应该工作:
#!/bin/sh DIR="/path/to/your/files" Now=$(date +%s) DAYS=30 for file in "$DIR/"* do if [ $(((`stat $file -c '%Y'`) + (86400 * $DAYS))) -lt $Now ] then # process / rm / whatever the file... fi done
有点解释: stat <file> -c '%Z'给出文件的修改时间,自UNIX时代开始,文件$(date +%s)给出当前UNIX时间戳。 然后只是简单的检查一下,看看文件的时间戳加上7天的秒数是否大于当前的时间戳。
如果您更喜欢依赖文件名中的日期,则可以使用此例程来检查日期是否比另一个更早:
is_older(){ local dtcmp=`date -d "$1" +%Y%m%d`; shift local today=`date -d "$*" +%Y%m%d` return `test $((today - dtcmp)) -gt 0` }
然后你可以遍历文件名,传递'-7天'作为第二个日期:
for filename in *; do dt_file=`echo $filename | grep -o -E '[12][0-9]{3}(-[0-9]{2}){2}'` if is_older "$dt_file" -7 days; then # rm $filename or whatever fi done
在is_older例程中, date -d "-7 days" +%Y%m%d将返回前7天的日期,以数字格式准备进行比较。
DIR='' Now=$(date +%s) for file in "$DIR/"* do echo $(($(stat "$file" -c '%Z') + $((86400 * 7)))) echo "----------" echo $Now
DONE
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。