Delete old version folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete old version folder
# 1  
Old 06-23-2010
Delete old version folder

In one directory,I have 4 folders with names as version numbers as 1.0.0.11 1.0.0.12 1.0.0.13 1.0.0.13a
I have to keep the latest one and delete the older ones.I can't use code to delete the files that are n days old as these folders can be of same day...1 day before or 2 mnths before...

Can anybody help me developing this script..one of my automation tool needs this script and I m trying to develop it since a long time...
# 2  
Old 06-23-2010
I assume 1.0.0.13a is newer than 1.0.0.13.

If so, you can consider the below:
Code:
leion [454]> ls
1.0.0.11    1.0.0.12    1.0.0.13    1.0.0.13a
leion [455]> ls | sort
1.0.0.11
1.0.0.12
1.0.0.13
1.0.0.13a
leion [456]> ls | sort | sed '$d'
1.0.0.11
1.0.0.12
1.0.0.13
leion [457]> ls | sort | sed '$d' | xargs rm -rf {} \;
leion [458]> ls
1.0.0.13a

# 3  
Old 06-23-2010
One more queston please

I really appreciate your reply...

one more question. If there is another directory with name "current" which is pointing one of the version folders. You can say that it's soft-linked with one of the version named folders.

I don't want current to be deleted as well as the version folder to which current is linked.

Any help in this regard please???????
# 4  
Old 06-23-2010
Based on Leion's code:

Code:
C=$( ls -l | grep 'curr' | awk -F "-> " '{print $NF}' ); \
  ls -1 | sort | sed -e "/$C/d" -e '$d' | xargs rm -rf {} \;

# 5  
Old 06-23-2010
Try:
Code:
ls -d 1.* | sort -nt. | sed '$d' | xargs rm -rf

Note that this will not work with names that contain spaces..

@Leion, dr.house I think xargs got mixed up with find syntax a bit there, no?

For directory names with spaces one could use this:
Code:
ls -d 1.* | sort -nt. | sed '$d' |  while read line; do rm -rf "$line"; done


Last edited by Scrutinizer; 06-23-2010 at 12:49 PM.. Reason: Thought this was about files, but they are directories
# 6  
Old 06-23-2010
Thanks alot guys....

Still I am not getting to the solution..

@Leion,Your command is working but I need to keep a folder named Current also which is soft linked to the latest version. A little modification in your code will work.Please reply if you can..

@dr.House,with your code line,only current folder is remaining but I need to keep a folder named Current also which is soft linked to the latest version.Please give your valuable suggestion.

@Scrutinizer,with your code,only the latest version folder is remaining but not the one with the name Current.
# 7  
Old 06-23-2010
Code:
C1='Current'; C2=$( ls -l | grep $C1 | awk -F "-> " '{print $NF}' ); \
  ls -1 | sort | sed -e "/$C1/d" -e "/$C2/d" -e '$d' | xargs rm -rf {} \;

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

How to delete other's folders which are in our own folder?

Hi All, I need a solution for the following scenario. I am the owner for the particular folder and I have given 777 permissions for some specific reasons. So others can able to create folders and files. But when I am done with the work, I need to delete the folders which are created by... (4 Replies)
Discussion started by: manoj_thorali
4 Replies

4. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: jasperux
2 Replies

5. UNIX for Dummies Questions & Answers

Getting a process/program version from /proc folder

Hello I am writing a script that will first execute ps to get the list of processes running, and the go into the /proc folder for each PID listed and gather relevant information. I looked through the contents of a particular process in the /proc folder and I can't find where I can locate... (2 Replies)
Discussion started by: flagman5
2 Replies

6. Shell Programming and Scripting

Folder permissions to delete

I am using the below command to delete files from directories and subdirectories find /test/abc/xyx -type f -mtime +7 -exec rm -f {} \; there are some subfolders in xyx for which i don't have permission to delete. Is there a way i can check the permission of the folder first and then delete... (4 Replies)
Discussion started by: ch33ry
4 Replies

7. Shell Programming and Scripting

Auto delete the folder

Hi, i have the directory structure directory /home/ncs/controller/logs/ in this path i have following directories cl03032010 cl04032010 cl05032010 cl06042010 i want to delete the folders which are 2 weeks old.. through the crontab (2 Replies)
Discussion started by: mail2sant
2 Replies

8. UNIX for Dummies Questions & Answers

Delete file from folder

Hi all, I amd new in UNIX programming. I have a query. I need to delete some files (like .dec files) from some folders. I have a list of folders. What will be command for it. Please help me. Thanks in Advance. (6 Replies)
Discussion started by: eclairs
6 Replies

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

10. UNIX for Dummies Questions & Answers

How can i delete files in folder by date?

Hi, I have some files on a folder and i want to delete all the files that were created on July. Thanks, Kobi. (9 Replies)
Discussion started by: kobibn
9 Replies
Login or Register to Ask a Question