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

所属分类: 脚本专栏 / linux shell 阅读数: 544
收藏 0 赞 0 分享

复制代码 代码如下:

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

更多精彩内容其他人还在看

php编译安装常见错误大全和解决方法

这篇文章主要介绍了php编译安装常见错误大全和解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux base shell重定向详解

这篇文章主要介绍了Linux base shell重定向的相关资料,并用一个简明例子总结了常见用法(在第三节),需要的朋友可以参考下
收藏 0 赞 0 分享

Linux Shell 常见的命令行格式简明总结

这篇文章主要介绍了Linux Shell 常见的命令行格式简明总结,非常实用,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell 命令替换的两种方式

这篇文章主要介绍了Shell 命令替换的两种方式,需要的朋友可以参考下
收藏 0 赞 0 分享

Python创建、删除桌面、启动组快捷方式的例子分享

这篇文章主要介绍了Python创建、删除桌面、启动组快捷方式的例子分享,需要的朋友可以参考下
收藏 0 赞 0 分享

shell基础学习中的字符串操作、for循环语句示例

这篇文章主要介绍了shell基础学习中的字符串操作、for循环语句示例
收藏 0 赞 0 分享

shell脚本中28个特殊字符的作用简明总结

这篇文章主要介绍了shell脚本中28个特殊字符的作用简明总结,需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell流程控制语句实例讲解(if、for、while、case语句实例)

linux shell有一套自己的流程控制语句,其中包括条件语句(if),循环语句(for,while),选择语句(case)。下面我将通过例子介绍下,各个语句使用方法
收藏 0 赞 0 分享

分享一个实用的iptables脚本(各种过滤写法参考)

这篇文章主要介绍了分享一个实用的iptables脚本(各种过滤写法参考),需要的朋友可以参考下
收藏 0 赞 0 分享

shell脚本实现ssh自动登录功能分享

mac下没有找到好用的类似secureCRT,就自己写了个自动登录的脚本,分享一下,如果是新浪的,就基本不用修改代码就直接能用
收藏 0 赞 0 分享
查看更多