Deleting folder with Date format


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Deleting folder with Date format
# 1  
Old 11-03-2011
Deleting folder with Date format

Hi,
I have number of directories being created under a main directory on daily basis with the sysdate as below
Code:
$ pwd
/hello/TEST
$ ls
20111103
20111102
20111101
20111031
20111030
20111029
20111028
20111027
20111026
20111025
20111024
20111023
20111022
20111021
20111020

All the above are directories under TEST.
Could someone please advise how can I delete the directory which are 30 days old.

That is if I run the command today(20111103) it should delete any directories older then 20111003 and so on.

Many Thanks In advance

Moderator's Comments:
Mod Comment Please use code tags <- click the link!

Last edited by zaxxon; 11-03-2011 at 11:08 AM.. Reason: code tags, see PM
# 2  
Old 11-03-2011
If you use the search function of the forum with the string "delete files older than" you will get:
The UNIX and Linux Forums - Search Results
# 3  
Old 11-03-2011
Hi ,
I have serched the forum but all I get is how to delete files from a folder.

But my query is to delete folders(as above) which are under a folder.

I am quite new to Unix. So any help is appriciated.
# 4  
Old 11-03-2011
Can you work with number of days?
Code:
# In this case it will look for directories from 30 until 50 (30 + 20) days ago!
countValBack=20
initVal=30
countValues=`expr ${initVal} + ${countValBack}`

basePath="./"

while [ ${countValues} -ge ${initVal} ]
do
	currDate=`date -d "${countValues} days ago" +"%Y%m%d"`
	bpDate="${basePath}/${currDate}"
	if [ -d "${bpDate}" ]
	then
		echo "Removing directory: [${bpDate}] - [${countValues}] days ago..."
		rm -rf "${bpDate}"
		if [ ${?} -eq 0 ]
		then
			echo "Removed successffully!"
		else
			echo "Failed to remove."
		fi
	fi
	
	countValues=`expr ${countValues} - 1`
done

If not, check this link: https://www.unix.com/answers-frequent...rithmetic.html, it may help you!

Also, to delete a directory with its contents, you can use: "rm -rf <dir>" but be careful with it!

I hope it helps!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

2. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

5. UNIX for Dummies Questions & Answers

Changing from Excel date format to MySQL date format

I have a list of dates in the following format: mm/dd/yyyy and want to change these to the MySQL standard format: yyyy-mm-dd. The dates in the original file may or may not be zero padded, so April is sometimes "04" and other times simply "4". This is what I use to change the format: sed -i '' -e... (2 Replies)
Discussion started by: figaro
2 Replies

6. Shell Programming and Scripting

Deleting the contents of a folder older than X hours

Every day a new .zip file is uploaded to a folder and at mid-night the zip file is to be extracted into a /data/ folder, inside a date-named folder. # This should extract the contents of a zip file into the /data/ folder into a date based folder /usr/bin/unzip -a -o... (15 Replies)
Discussion started by: worchyld
15 Replies

7. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

8. Shell Programming and Scripting

convert mmddyy date format to ccyyddd format??

hi, for reading a cobol indexed file i need to convert "mmddyy" date format to "ccyyddd" format. i checked the datecalc and other scripts but couldnt modify them to cater to my need:(... The datecalc gives an output which i believe is the total days till that date, but i want to convert it... (2 Replies)
Discussion started by: Bhups
2 Replies

9. Shell Programming and Scripting

deleting files in folder

helloo people... I was trying to do one script that would delete all files in one folder there are 2-3 usefull files in folder all other is junk.. how to say delete all except those 3 files... thanks in advance best regards, (10 Replies)
Discussion started by: amon
10 Replies

10. Shell Programming and Scripting

Deleting the file in a folder

hi everyone I am having some n number of files in folder I want delete a file which name has todays date example my file name is 14(todaysdate) when i want transfer files i want to delete that file which name has 14.i want delete only 14 file.need help ... (2 Replies)
Discussion started by: srivsn
2 Replies
Login or Register to Ask a Question