LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-19-2009, 01:48 PM   #1
laki47
Member
 
Registered: Feb 2009
Posts: 33

Rep: Reputation: 15
Please chech my statement - delete all except file with largest number extension


Hello!

I need to remove all files except one with the greatest number suffix.
I.E. I have:

...
tmp.123
tmp.124
tmp.125
...
tmp.226

Now, I wrote the statement to delete all but tmp.226 ->

ls -1 tmp.* |sort -t'.' -rn -k2 |sed -n '2,$p' |xargs rm -f

I've tried this and it works but I need some experienced opinion... Is there any simpler solution?

Many thanks!!!

Last edited by laki47; 02-19-2009 at 01:53 PM.
 
Old 02-19-2009, 02:30 PM   #2
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Well for a start you could squeeze a few chars out of that by using tail instead of sed (and taking ticks off around the dot in the delimiter option of sort; [EDIT] probably that "-1" for ls is not needed either):

Code:
ls tmp.* |sort -t. -rn -k2 |tail -n+2 |xargs rm -f
That saves you a total of 6 chars (plus 3 more, if you remove the spaces to the left of each pipe sign) of typing, if I got it right. Not much, but you asked for a simpler solution, and that's such: less typing and no dollar sign

Hopefully something better follows; I'm too tired to think of anything wise now. [EDIT] Except that you could use a time-based sorting rather than suffix-based, if you knew which file to preserve; you could touch it (thus making it the "newest" file in terms of ctime) and then remove all the "older" files:

Code:
touch tmp.226
ls -c tmp.* |tail -n+2 |xargs rm -f
That should remove all but the newest (as in ctime, time of last modification of file status information) file, the one you just touched -- tmp.226. The downside of this is obviously that you need to know the filename of the "last" file which you want to preserve; though if you're fine with checking it out with ls, for example, go ahead. A suffix-based method is better if you don't want to fiddle with timestamps or if you want it "fully automated", though, and in any case you should first check (run without "rm") what gets removed and what not before taking real action.

Last edited by b0uncer; 02-19-2009 at 02:53 PM.
 
Old 02-19-2009, 03:16 PM   #3
laki47
Member
 
Registered: Feb 2009
Posts: 33

Original Poster
Rep: Reputation: 15
Sorry, I forgot to tell that is not necceserily that i.e. file tmp.223 is older ther tmp.122. So, just say that I can't rely on time of modification...

Thank you for your answer, it's really simple solution (and I like them most )!
 
Old 02-20-2009, 06:02 AM   #4
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,061

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
All the examples you have given have three digits after the dot; will this always be true of every file that you compare?

(And note that, under Unixxy type systems that dot doesn't have a particular significance to the OS (it may have some significance to some particular app or your bash script) and temp.1234567890 is a valid filename, as is temp.123.456.789.0, temp.123.456.789.0.bak and temp.12 and so the, eg, .12 isn't an extension...the filename is the whole string of characters.)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to delete last number/word of a file and incude file count at the end of the chennaiguy Linux - Newbie 2 02-18-2008 09:08 PM
An idea for a survey: What country have the largest number of people prefer linux TruongAn LQ Suggestions & Feedback 2 09-11-2005 10:56 AM
JAVA: displaying largest number of an array bug randomx Programming 17 06-28-2004 07:59 PM
Largest Known prime number is discovered by MSU Grad! bolinux General 3 12-03-2003 07:33 PM
bash scripting - largest # file extension brian0918 Programming 3 06-16-2003 12:06 PM


All times are GMT -5. The time now is 06:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration