Delete all except latest datetime entery folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete all except latest datetime entery folder
# 1  
Old 10-20-2011
Delete all except latest datetime entery folder

Hello Expert,

I have a strange requirement,

I have folder where weekly backup are taken...
week1_bkup_02102011
week2_bkup_09102011
week3_bkup_16102011
.
.
everyweek a backup is created
I want to delete all folder except latest one

for example in above a week4_bkup_23102011 is added then it should remove all other three. only latest one should be present.

is there any logic then please help Smilie

Thanks
# 2  
Old 10-20-2011
Assuming the modification time of the directories is reliable to sort out the latest:
Code:
set -- $(ls -t)
shift
rm -r $*

If the "latest" directory is last in the sorted output of directory names, you can do:
Code:
set -- $(ls -r)
shift
rm -r $*

If both are not reliable, you can use an appropriate sort command, like:
Code:
set -- $(ls | sort -t@ -k 0.16,0.19 -k 0.14,0.15 -k 0.12,0.13)
shift
rm -r $*


Last edited by hergp; 10-21-2011 at 12:14 PM..
This User Gave Thanks to hergp For This Post:
# 3  
Old 10-20-2011
thanks a lot for your response
# 4  
Old 10-20-2011
Hope the below code will also help you.

add this code to your script or place it in cron. then every time this code runs, it will remove the file modified less than one day.

Code:
find \tmp -mtime -1 -exec rm -f {} \;

Please change directory to your own.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Retrieve the Latest file in a folder using SFTP

HI Guys, Can anyone help me to retrieve the latest file from below example using SFTP I have below list of files in a folder v403t.lstprgms.sortin1 val027.d099.fwest.oct2711 xcelrptd.d1400sqp.dec1713.t040459.@02del xcelrptd.d1400sqp.dec1713.t073308.@02del... (3 Replies)
Discussion started by: heye18
3 Replies

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

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

4. Shell Programming and Scripting

Find latest date in folder

HI I have folder in home dir. /home/kpp/07222013 /home/kpp/07212013 /home/kpp/07202013 Output :-- /home/kpp/07222013 Just find latest date (5 Replies)
Discussion started by: pareshkp
5 Replies

5. UNIX for Dummies Questions & Answers

Copy the latest (last file) in given folder

#!/bin/bash for i in {1..1536..1} do #find /home/test/Desktop/up111/workplace/Malware/$i/logs for a in /home/test/Desktop/up111/workplace/Malware/$i/logs/* do #max=a for b in /home/test/Desktop/up111/workplace/Malware/$i/logs/* do ... (4 Replies)
Discussion started by: upvan111
4 Replies

6. Shell Programming and Scripting

Find the latest folder

Hi, I want to find out the files that are created the recent, how can I do it? find . -type d ( i dont know what option to use to find the latest, it can be 1 day old or 10 days, but want to pick the latest one only) Thanks, (5 Replies)
Discussion started by: rudoraj
5 Replies

7. Shell Programming and Scripting

Delete Files Based on Datetime Stamp

I have a Unix directory, let's call it /home/id for example purposes. It contains the following files: oldfile.txt.20091101, oldfile.txt.20091102, oldfile.txt.20091103, etc. I am trying to create a Korn Shell script that will go to /home/id and delete any oldfile.txt that has a datetime stamp... (1 Reply)
Discussion started by: ijmoore
1 Replies

8. Shell Programming and Scripting

delete all the files in folder a which exist in folder b

Hi , I need a script which basically deltes all files in folder a which are alreasy present in folder b say folder a has files abc.txt pqr .txt and b has abc.txt pqr.txt rmr.txt then file abc.txt and pqr.txt from a should be deleted (6 Replies)
Discussion started by: viv1
6 Replies

9. Shell Programming and Scripting

Copying latest file into a folder

Hello all, this is my first post while i am trying to understand unix. I would basically like to know if i can do this: Lets say i have a folderA and folderB And i save something in folderA Can i make a script that checks folderA latest file, then compares it with the date of latest file in... (16 Replies)
Discussion started by: takissd
16 Replies

10. UNIX for Dummies Questions & Answers

Copy the latest file from a folder

Hi, I have a problem. I have some text files in a folder. The names can be like: emp_20080307053015.dat emp_20080306053015.dat emp_20080305053015.dat emp_20080304053015.dat The date format appended is like yyyymmdd and timestamp. What i need is i have to copy the latest file every... (3 Replies)
Discussion started by: Aswarth
3 Replies
Login or Register to Ask a Question