LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Removing files except the last 5 created (https://www.linuxquestions.org/questions/programming-9/removing-files-except-the-last-5-created-574393/)

tovohery 08-03-2007 12:54 AM

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

raskin 08-03-2007 01:04 AM

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.

syg00 08-03-2007 01:23 AM

Might be an idea to use "ls -cr" in place of "ls" ...

b0uncer 08-03-2007 01:38 AM

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

raskin 08-03-2007 01:40 AM

It has '!' operation. But anyway you have to find fifth file from the end somehow.

tovohery 08-03-2007 01:57 AM

Yes the files are created in this order and increment automatically.

Thanks

theYinYeti 08-03-2007 02:18 AM

Code:

ls -1t telma_1_*.ARC | tail --lines=+6 | while read f; do rm -f "$f"; done
Yves.

ta0kira 08-03-2007 12:05 PM

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

raskin 08-03-2007 03:13 PM

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.

theNbomr 08-03-2007 06:26 PM

Code:

rm `ls | sort | tail -6`

--- rod

raskin 08-04-2007 01:32 AM

Seems that this will always delete 6 files.

syg00 08-04-2007 01:38 AM

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


All times are GMT -5. The time now is 03:56 AM.