Shell script Implementation deletes a year ago file feature sharing _linux shell

Source: Internet
Author: User

Copy Code code as follows:

#!/bin/bash
#Description: Delete Files
#===== defines the current year, the month, and the directory where the file resides =====#
Currentyear= ' Date +%y '
Currentmonth= ' date +%m |awk-f ' 0 ' {print $} '
Videodir=/var/video
#====== defines a transformation function that converts a month abbreviation to a number representation ========#
function Month2num () {
Case $file _month in
.)
File_month=1
;;
FEB)
file_month=2
;;
Mar)
File_month=3
;;
APR)
File_month=4
;;
May)
File_month=5
;;
June)
File_month=6
;;
June)
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
}
#===== defines the previous year as well as the list of all files from previous years =====#
lastyear=$[$currentYear-1]
Lastyearfiles= '/bin/ls-l $videodir/|grep $lastYear |awk-f ' ' {print $} '
#===== deletes previous year's file, today is May 2013, deletes all documents prior to May 2012 (January-April) =====#
For LastFile in ' Echo $lastYearFiles '
Todo
File_month= '/bin/ls-l $videodir/$lastfile |awk-f ' ' {print $} '
Month2num
If [$file _month-lt $currentMonth]
Then
RM-RF $videodir/$lastfile
Fi
Done
#===== Delete all files not in the previous year and not this year =====#
Otheryearfiles= '/bin/ls-l $videodir/|grep-v $lastYear |awk-f ' ' {print $} '
For Otherfile in ' Echo $otherYearFiles '
Todo
file_year_format= '/bin/ls-l $videodir/$otherfile |awk-f ' {print $} ' |wc-c '
If [$file _year_format-eq 5]
Then
RM-RF $videodir/$otherfile
Fi
Done

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.