Delete folder in a directory which are two months old


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete folder in a directory which are two months old
# 1  
Old 07-06-2012
Delete folder in a directory which are two months old

I need help. I have to create a script which will delete multiple directory (folders) that are two months old. Example, I have folders dated April, May, June and July. I have to delete folders April and May and retain June and July and as such that when the July month lapse, the folder June will be deleted.


Thanks for the help.
# 2  
Old 07-06-2012
There are lots of threads related to same topic.
Please use search. You can also have a look at the thread listing at the bottom of this page with the similar topic.
# 3  
Old 07-06-2012
below code will give directories older than two months, means not JUN and not JUL.

Code:
prev_mth=`date --date="last month" +%b`
curr_mth=`date "+%b"`
ls -lth | grep "^d" | awk -v CM=$curr_mth -v PM=$prev_mth  ' $6 != CM  { if ( $6 != PM ) print $9 }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete oldest folder based on folder named as date

Hi, I have a script doing backup to synology server, the script create new folder each day with the date as being folder name i.e. 2018-07-30. Just before creating the new folder I want the script to find the oldest folder from the list and delete it including its content. for example... (3 Replies)
Discussion started by: humble_learner
3 Replies

2. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

3. Shell Programming and Scripting

Deleting files and directory's older than 3 months

I have a qnap TS259 that is running ubuntu. Have successfully setup back scripts that are initiated by cron. I would like to create a couple scrypts that would operate on the recycle bins for both drives. Just want to be able to run the script manually that would walk through both directories... (13 Replies)
Discussion started by: mackconsult
13 Replies

4. Red Hat

Need Script to ZIP/SAVE & then DELETE Log file & DELETE ZIPS older than 12 months

ENVIROMENT Linux: Fedora Core release 1 (Yarrow) iPlanet: iPlanet-WebServer-Enterprise/6.0SP1 Log Path: /usr/iplanet/servers/https-company/logs I have iPlanet log rotation enabled rotating files on a daily basis. The rotated logs are NOT compressed & are taking up too much space. I... (7 Replies)
Discussion started by: zachs
7 Replies

5. Shell Programming and Scripting

Delete 2 months before old files when we get alarm directory size > 90%

Please Help me Question: - we manually moving/removing old files, when we are getting alarm as /dir1/dir2 size greater than 90%. we manually moving/removing old files and reduce the file size to less than 90% - I want to delete all 2months before old files once we get the thresh hold >= 90%... (3 Replies)
Discussion started by: piyus
3 Replies

6. Shell Programming and Scripting

Looking for improvement to script that compresses a directory that is 2 months old

I write reports daily to a directory that is named with the month and year. I wanted to compress all but the last two months worth of these directories to save space. I am going to cron a job to run on the first of each month to do this clean up. I didn't have zip/unzip on my AIX environment so... (0 Replies)
Discussion started by: slatoms
0 Replies

7. UNIX for Dummies Questions & Answers

single line command to delete a 6 months old file

i had this scenario where i need to delete a file that is 6 months old which is no longer needed. basically the filename is in the format of PCARDDAILYmmddyyyy.txt where the mm is the month, dd is the day, and yyyy is the year. e.g. PCARDDAILY05262009.txt PCARDDAILY05252009.txt ... (6 Replies)
Discussion started by: wtolentino
6 Replies

8. Shell Programming and Scripting

Delete 6 months old files

Hi, Iam trying to delete 6 months old files. Iam using the following script -ctime +190 -type f -exec echo rm {} \; Iam getting an error saying -ctime not found. (6 Replies)
Discussion started by: Sompalle Jalaja
6 Replies

9. Shell Programming and Scripting

Delete files older than 3 months.(read date from the name of the file)

Guys, My log files stored in the date format format below(log_20080714072942): TIMESTAMP=`date +%Y%m%d%H%M%S` LOG=/log/log_${TIMESTAMP}.log I'm looking for a shell script which deletes all files which is older than 3 months from today. Regards, Bhagat (3 Replies)
Discussion started by: bhagat.singh-j
3 Replies

10. UNIX for Advanced & Expert Users

Delete database table based on months using script.

HI All, Newbie here. I have a script which generates the tables in teradata database with time stamp like below.I want to add some more code to delete the archive tables which are 2months old. . $HOME/.profile t_name=$1 procdate=`date +%y%m%d` log_dir=$Folder/log tab=`echo... (0 Replies)
Discussion started by: Maverick79
0 Replies
Login or Register to Ask a Question