LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-03-2007, 12:54 AM   #1
tovohery
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Rep: Reputation: 0
Removing files except the last 5 created


Hi all,
I'm new in linux, could someone has a script to remove all file in a directory except the last 5 created files.
The below is the file name

telma_1_1260.ARC
telma_1_1261.ARC
telma_1_1262.ARC
telma_1_1263.ARC
telma_1_1264.ARC
telma_1_1265.ARC
telma_1_1266.ARC
telma_1_1267.ARC
telma_1_1268.ARC
telma_1_1269.ARC
telma_1_1270.ARC
telma_1_1271.ARC
telma_1_1272.ARC
telma_1_1273.ARC
telma_1_1274.ARC
telma_1_1275.ARC
telma_1_1276.ARC
telma_1_1277.ARC
telma_1_1278.ARC
telma_1_1279.ARC
telma_1_1280.ARC
telma_1_1281.ARC
telma_1_1282.ARC
telma_1_1283.ARC
telma_1_1284.ARC
telma_1_1285.ARC
telma_1_1286.ARC
telma_1_1287.ARC
telma_1_1288.ARC
telma_1_1289.ARC
telma_1_1290.ARC
telma_1_1291.ARC
telma_1_1292.ARC
telma_1_1293.ARC

Any help?
Regards
 
Old 08-03-2007, 01:04 AM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Is it safe to assume that they are created in this order and there are no other files?
Code:
count=$(ls | wc -l)
will count lines.
Code:
ls | head -$((count -  5))
after it will output all lines of ls except last five.
Code:
ls | head -$((count -  5)) | xargs rm
will remove the files previous command listed. Read man pages for bash, head, xargs.
 
Old 08-03-2007, 01:23 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Might be an idea to use "ls -cr" in place of "ls" ...
 
Old 08-03-2007, 01:38 AM   #4
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
***sorry***
I didn't read it well enough; you were talking about the last five items, not items modified in certain time..

How about producing a list with find (or some other tool) and sort it by creation time, then pick up last five elements and tell rm to delete them?

Last edited by b0uncer; 08-03-2007 at 01:43 AM.
 
Old 08-03-2007, 01:40 AM   #5
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
It has '!' operation. But anyway you have to find fifth file from the end somehow.
 
Old 08-03-2007, 01:57 AM   #6
tovohery
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Original Poster
Rep: Reputation: 0
Yes the files are created in this order and increment automatically.

Thanks
 
Old 08-03-2007, 02:18 AM   #7
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Code:
ls -1t telma_1_*.ARC | tail --lines=+6 | while read f; do rm -f "$f"; done
Yves.
 
Old 08-03-2007, 12:05 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I had something similar:
Code:
[ `ls -1 | grep -c .` -gt 5 ] && rm -f $(ls -t1 | tail -n$(expr `ls -1 | grep -c .` - 5))
All versions should really be broken down to avoid multiple ls calls since the number of files could change between the two calls.
ta0kira
 
Old 08-03-2007, 03:13 PM   #9
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Technically, if you have n files and remove first n-5, then you can just pretend to read task as 'remove files existing in the moment of launch (with possible delay to read list of them) except last five of them'. So it is at least safe to do.
 
Old 08-03-2007, 06:26 PM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Code:
rm `ls | sort | tail -6`

--- rod
 
Old 08-04-2007, 01:32 AM   #11
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Seems that this will always delete 6 files.
 
Old 08-04-2007, 01:38 AM   #12
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Gotta say, Yves solution looks the most elegant.
Simple, easy to understand - what's not to like.
 
  


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
removing last created files Prasun1 Linux - General 7 09-25-2005 07:31 PM
Files being created with GMT timestamp Locura Slackware 0 12-07-2004 08:54 AM
Removing files wihtout removing containing Direcotry caps_phisto Linux - General 2 10-07-2004 08:16 AM
If I delete files are new ones created?? BajaNick Linux - Software 1 07-06-2004 11:07 PM
how are /dev files created? jkobrien Linux - Hardware 3 10-17-2003 06:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:10 AM.

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
Open Source Consulting | Domain Registration