您的位置:首页 > 运维架构 > Shell

Shell脚本实现删除一年前文件功能分享

2018-10-12 13:57 781 查看


#!/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

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: