700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > linux删除一年前的文件 Shell脚本实现删除一年前文件功能分享

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

时间:2022-06-29 19:09:27

相关推荐

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

#!/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}'`

#===== 删除上一年的文件,今天为5月,则删除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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。