uday26
June 23, 2010, 6:21am
1
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...
Leion
June 23, 2010, 7:49am
2
I assume 1.0.0.13a is newer than 1.0.0.13.
If so, you can consider the below:
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
uday26
June 23, 2010, 8:28am
3
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???????
Based on Leion's code:
C=$( ls -l | grep 'curr' | awk -F "-> " '{print $NF}' ); \
ls -1 | sort | sed -e "/$C/d" -e '$d' | xargs rm -rf {} \;
Try:
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:
ls -d 1.* | sort -nt. | sed '$d' | while read line; do rm -rf "$line"; done
uday26
June 23, 2010, 11:06am
6
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.
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 {} \;
ls -d 1.* |egrep -v "$(ls -ld Current | awk '{print $NF}' )|$(ls -d 1.* | sort -nt. | tail -1)" | xargs rm -rf
Leion
June 28, 2010, 1:28am
9
Sorry Something cropped up and I did not check my emails.
are you saying your current link is already pointing to the latest version?
if so you can try this:
ls | grep -v Current | sort | sed '$d' | xargs rm -rf
If not, you can try this:
ls | grep -v Current | grep -v `ls -l Current | awk '{print $NF}'` | sort | sed '$d' | xargs rm -rf
Sorry for the double grep -v. Maybe someone can help to combine them..
---------- Post updated at 01:28 PM ---------- Previous update was at 01:09 PM ----------
scrutinizer:
Try:
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:
ls -d 1.* | sort -nt. | sed '$d' | while read line; do rm -rf "$line"; done
oh yar thanks for pointing out the syntax part
You could combine like so for instance:
ls | egrep -v "Current$|$(ls -l Current | awk '{print $NF}')$"
The $ character is important to make sure that the patterns the grep matches are at the end of the line.
Leion
June 28, 2010, 3:39am
11
scrutinizer:
You could combine like so for instance:
ls | egrep -v "Current$|$(ls -l Current | awk '{print $NF}')$"
The $ character is important to make sure that the patterns the grep matches are at the end of the line.
The Current$ does not work for me as I used a alias to do a
ls -F
leion > ls -ltr
total 0
0 lrwxrwxrwx 1 leion group 8 Jun 28 17:33 Current -> 1.0.0.12
0 -rw-r--r-- 1 leion group 0 Jun 28 17:33 1.0.0.13a
0 -rw-r--r-- 1 leion group 0 Jun 28 17:33 1.0.0.13
0 -rw-r--r-- 1 leion group 0 Jun 28 17:33 1.0.0.12
0 -rw-r--r-- 1 leion group 0 Jun 28 17:33 1.0.0.11
leion > ls | egrep -v "Current$|$(ls -l Current | awk '{print $NF}')$"
1.0.0.11
1.0.0.13
1.0.0.13a
Current@
leion > ls | egrep -v "Current|$(ls -l Current | awk '{print $NF}')$"
1.0.0.11
1.0.0.13
1.0.0.13a
However the combine of my twin grep -v using egrep -v helps.
Thank you for sharing.
Hi, you're welcome...
To temporarily bypass an alias you can use a backslash:
\ls | egrep -v "Current$|$(ls -l Current | awk '{print $NF}')$"
Leion
June 28, 2010, 3:54am
13
scrutinizer:
Hi, you're welcome...
To temporarily bypass an alias you can use a backslash:
\ls | egrep -v "Current$|$(ls -l Current | awk '{print $NF}')$"
Nice!
Learnt another thing from you today