手机
当前位置:查字典教程网 >脚本专栏 >linuxshell >Shell脚本实现删除一年前文件功能分享
Shell脚本实现删除一年前文件功能分享
摘要:复制代码代码如下:#!/bin/bash#Description:deletefiles#=====定义当前年份,月份以及文件所在目录===...

复制代码 代码如下:

#!/bin/bash

#Description: delete files

#=====定义当前年份,月份以及文件所在目录=====#

currentYear=`date +%Y`

currentMonth=`date +%m |awk -F'0' '{print $2}'`

videodir=/var/video

#======定义一个转换函数,用于将月份缩写转换为数字表示========#

function month2num(){

case $file_month in

Jan)

file_month=1

;;

Feb)

file_month=2

;;

Mar)

file_month=3

;;

Apr)

file_month=4

;;

May)

file_month=5

;;

Jun)

file_month=6

;;

Jul)

file_month=7

;;

Aug)

file_month=8

;;

Sep)

file_month=9

;;

Oct)

file_month=10

;;

Nov)

file_month=11

;;

Dec)

file_month=12

;;

*)

echo "Oh,Are you kidding me"

exit 1

;;

esac

}

#=====定义上一年年份以及上一年的所有文件列表=====#

lastYear=$[$currentYear-1]

lastYearFiles=`/bin/ls -l $videodir/ |grep $lastYear |awk -F' ' '{print $9}'`

#===== 删除上一年的文件,今天为2013年5月,则删除2012年5月之前的所有文件(1-4月)=====#

for lastfile in `echo $lastYearFiles`

do

file_month=`/bin/ls -l $videodir/$lastfile |awk -F' ' '{print $6}'`

month2num

if [ $file_month -lt $currentMonth ]

then

rm -rf $videodir/$lastfile

fi

done

#=====删除非上一年以及非今年的所有文件=====#

otherYearFiles=`/bin/ls -l $videodir/ |grep -v $lastYear |awk -F' ' '{print $9}'`

for otherfile in `echo $otherYearFiles`

do

file_year_format=`/bin/ls -l $videodir/$otherfile |awk -F' ' '{print $8}'|wc -c`

if [ $file_year_format -eq 5 ]

then

rm -rf $videodir/$otherfile

fi

done

【Shell脚本实现删除一年前文件功能分享】相关文章:

Shell脚本实现的一个简易Web服务器例子分享

Shell脚本实现硬盘空间和表空间的使用情况统计并邮件通知

Shell脚本定义变量和重新赋值

Shell脚本实现的阳历转农历代码分享

25个好用的Shell脚本常用命令分享

shell脚本中使用iconv实现批量文件转码的代码分享

CentOS中使用Shell脚本实现每天自动备份网站文件和数据库并上传到FTP中

Shell脚本对比两个文本文件找出不同行的2个方法分享

Shell脚本实现的猜数字小游戏

Shell脚本学习指南之文本处理工具

精品推荐
分类导航