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
您可能感兴趣的文章:
相关文章推荐
- Shell脚本实现删除一年前文件功能分享
- shell脚本实现查找文件夹下重复的文件,并提供删除功能
- shell脚本实现查找文件夹下重复的文件,并提供删除功能
- shell脚本实现文件锁功能
- Shell脚本实现检测文件是否被修改过代码分享
- shell脚本中使用iconv实现批量文件转码的代码分享
- shell脚本中使用iconv实现批量文件转码的代码分享
- 用shell脚本实现删除一定大小的文件
- shell脚本实现拷贝大文件显示百分比的代码分享
- shell脚本实现删除文件的源代码
- shell脚本编程实现计算器功能并根据选择将其保存于文件中
- shell脚本实现ssh自动登录功能分享
- Linux下Shell实现当文件大于某size时候删除功能
- shell脚本实现文件锁功能
- Shell脚本实现自动修改IP、主机名等功能分享
- Shell脚本实现复制文件到多台服务器的代码分享
- Ruby实现的一个强大的批量删除文件脚本分享
- shell备份文件、重命名文件、删除指定文件的3个脚本分享
- shell备份文件、重命名文件、删除指定文件的3个脚本分享