# Delete old version folder

**URL:** <https://community.unix.com/t/delete-old-version-folder/268193>\
**Category:** Shell Programming and Scripting\
**Created:** [June 23, 2010, 6:21am UTC](https://community.unix.com/t/delete-old-version-folder/268193 "2010-06-23T06:21:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![uday26](https://community.unix.com/letter_avatar/uday26/32/6_22734026db12803ebb3e059622ac3534.png) [@uday26](https://community.unix.com/u/uday26)\
**Post date:** [June 23, 2010, 6:21am UTC](https://community.unix.com/t/delete-old-version-folder/268193/1 "2010-06-23T06:21:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Leion](https://community.unix.com/user_avatar/community.unix.com/leion/32/4676_2.png) [@Leion](https://community.unix.com/u/Leion)\
**Post date:** [June 23, 2010, 7:49am UTC](https://community.unix.com/t/delete-old-version-folder/268193/2 "2010-06-23T07:49:17Z")

</div>

I assume 1.0.0.13a is newer than 1.0.0.13.

If so, you can consider the below:

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![uday26](https://community.unix.com/letter_avatar/uday26/32/6_22734026db12803ebb3e059622ac3534.png) [@uday26](https://community.unix.com/u/uday26)\
**Post date:** [June 23, 2010, 8:28am UTC](https://community.unix.com/t/delete-old-version-folder/268193/3 "2010-06-23T08:28:24Z")

</div>

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???????

---

<div class="post-metadata">

**Author:** ![dr.house](https://community.unix.com/letter_avatar/dr.house/32/6_22734026db12803ebb3e059622ac3534.png) [@dr.house](https://community.unix.com/u/dr.house)\
**Post date:** [June 23, 2010, 9:08am UTC](https://community.unix.com/t/delete-old-version-folder/268193/4 "2010-06-23T09:08:27Z")

</div>

Based on Leion's code:

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

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [June 23, 2010, 9:25am UTC](https://community.unix.com/t/delete-old-version-folder/268193/5 "2010-06-23T09:25:04Z")

</div>

Try:

```nohighlight
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:

```nohighlight
ls -d 1.* | sort -nt. | sed '$d' | while read line; do rm -rf "$line"; done

```

---

<div class="post-metadata">

**Author:** ![uday26](https://community.unix.com/letter_avatar/uday26/32/6_22734026db12803ebb3e059622ac3534.png) [@uday26](https://community.unix.com/u/uday26)\
**Post date:** [June 23, 2010, 11:06am UTC](https://community.unix.com/t/delete-old-version-folder/268193/6 "2010-06-23T11:06:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dr.house](https://community.unix.com/letter_avatar/dr.house/32/6_22734026db12803ebb3e059622ac3534.png) [@dr.house](https://community.unix.com/u/dr.house)\
**Post date:** [June 23, 2010, 11:17am UTC](https://community.unix.com/t/delete-old-version-folder/268193/7 "2010-06-23T11:17:03Z")

</div>

```nohighlight
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 {} \;

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [June 23, 2010, 12:24pm UTC](https://community.unix.com/t/delete-old-version-folder/268193/8 "2010-06-23T12:24:08Z")

</div>

```nohighlight
 ls -d 1.* |egrep -v "$(ls -ld Current | awk '{print $NF}' )|$(ls -d 1.* | sort -nt. | tail -1)" | xargs rm -rf

```

---

<div class="post-metadata">

**Author:** ![Leion](https://community.unix.com/user_avatar/community.unix.com/leion/32/4676_2.png) [@Leion](https://community.unix.com/u/Leion)\
**Post date:** [June 28, 2010, 1:28am UTC](https://community.unix.com/t/delete-old-version-folder/268193/9 "2010-06-28T01:28:55Z")

</div>

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:

```plaintext
ls | grep -v Current | sort | sed '$d' | xargs rm -rf 

```

If not, you can try this:

```plaintext
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:
> 
> ```plaintext
> 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:
> 
> ```plaintext
> ls -d 1.* | sort -nt. | sed '$d' | while read line; do rm -rf "$line"; done
> 
> ```

oh yar thanks for pointing out the syntax part 🙂

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [June 28, 2010, 3:29am UTC](https://community.unix.com/t/delete-old-version-folder/268193/10 "2010-06-28T03:29:50Z")

</div>

> [@leion](#):
>
> Maybe someone can help to combine them..

You could combine like so for instance:

```plaintext
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.

---

<div class="post-metadata">

**Author:** ![Leion](https://community.unix.com/user_avatar/community.unix.com/leion/32/4676_2.png) [@Leion](https://community.unix.com/u/Leion)\
**Post date:** [June 28, 2010, 3:39am UTC](https://community.unix.com/t/delete-old-version-folder/268193/11 "2010-06-28T03:39:45Z")

</div>

> [@scrutinizer](#):
>
> You could combine like so for instance:
> 
> ```plaintext
> 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

```plaintext
ls -F

```

```plaintext
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. 🙂

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [June 28, 2010, 3:52am UTC](https://community.unix.com/t/delete-old-version-folder/268193/12 "2010-06-28T03:52:04Z")

</div>

Hi, you're welcome...  
To temporarily bypass an alias you can use a backslash:

```nohighlight
\ls | egrep -v "Current$|$(ls -l Current | awk '{print $NF}')$"

```

---

<div class="post-metadata">

**Author:** ![Leion](https://community.unix.com/user_avatar/community.unix.com/leion/32/4676_2.png) [@Leion](https://community.unix.com/u/Leion)\
**Post date:** [June 28, 2010, 3:54am UTC](https://community.unix.com/t/delete-old-version-folder/268193/13 "2010-06-28T03:54:43Z")

</div>

> [@scrutinizer](#):
>
> Hi, you're welcome...  
> To temporarily bypass an alias you can use a backslash:
> 
> ```plaintext
> \ls | egrep -v "Current$|$(ls -l Current | awk '{print $NF}')$"
> 
> ```

Nice! :b:  
Learnt another thing from you today
