源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-07-08 17:37 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Shell脚本实现删除一年前文件功能分享
[u]复制代码[/u] 代码如下:
#!/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)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部