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}'`

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值