Removing older version files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Removing older version files

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-05-2013
Arun Mishra Arun Mishra is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 11 June 2014, 4:30 AM EDT
Posts: 51
Thanks: 4
Thanked 0 Times in 0 Posts
Removing older version files

Hi,

I have a direcory as mentioned below:
/development/arun/cycdt/
unser the above i have directories
/2013
/2012
/2011
......
.....
/2000
I need to write a script which can delete the nth version of the directories.
as in if n=10 then the script should arrange the directories in descending creation order and delete all the directories apart from the recent 10 directories

Any help appreciated!!!
Sponsored Links
    #2  
Old 02-05-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 26 October 2014, 10:18 AM EDT
Location: India
Posts: 1,832
Thanks: 25
Thanked 526 Times in 505 Posts

Code:
#! /bin/bash

n=$1
x=`ls -l | wc -l`
for y in $(ls -1t | tail -$(( $x - $n ))); do rm -rf $y; done

The Following User Says Thank You to balajesuri For This Useful Post:
Arun Mishra (02-05-2013)
Sponsored Links
    #3  
Old 02-05-2013
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 973 Times in 789 Posts
Quote:
Originally Posted by balajesuri View Post
Code:
#! /bin/bash

n=$1
x=`ls -l | wc -l`
for y in $(ls -1t | tail -$(( $x - $n ))); do rm -rf $y; done

Since ls is run twice, there's a race condition. If a directory is deleted in the meantime, a directory that should have been kept will be nuked. Similarly, if a directory is added in the meantime, a directory that should have been removed will persist.

There is no need to run ls twice. You can just use tail's ability to index releative to the beginning of the data, tail -n +10 versus tail -n 10 . However, this approach still requires some arithmetic, since skipping the first x lines requires an option argument of x+1.

I wouldn't bother with tail. In my opinion, the simplest solution is to use sed:

Code:
ls -t | sed 1,10d | xargs rm -rf

Note that xargs does not play well with filenames containing whitespace or quotes. If such filenames occur, instead of xargs, a less efficient while-read loop would be necessary.

Code:
ls -t | sed 1,10d | while IFS= read -r dirname; do rm -fr "$dirname"; done

Regards,
Alister

Or, eve
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
For loop in older version of ksh RahulJoshi Shell Programming and Scripting 2 05-16-2011 09:47 AM
removing files older than n days big123456 UNIX for Advanced & Expert Users 2 01-21-2011 04:41 AM
Removing files older than one week in a directory sudhakaryadav Shell Programming and Scripting 2 05-25-2009 07:02 AM
Removing files older than 7 days texasoeb UNIX for Dummies Questions & Answers 3 04-20-2007 04:04 PM
removing files after 6 hours or older gthokala Shell Programming and Scripting 3 08-22-2005 10:59 PM



All times are GMT -4. The time now is 04:24 AM.