欢迎来到站长天空!

其它服务器

当前位置: 主页 > 服务器 > 其它服务器

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

时间:2025-12-31 08:30:02|栏目:其它服务器|点击:

复制代码 代码如下:
#!/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脚本实现apache日志中的状态码分析

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

本文地址:https://zz.feitang.co/server/34761.html

广告投放 | 联系我们 | 版权申明

申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:257218569 | 邮箱:257218569@qq.com

Copyright © 2018-2025 站长天空 版权所有 Powered by EyouCms冀ICP备14023439号