|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|