I have a directory of backups that looks like this:

-rw-r--r--   1 ftpuser ftponly   5610595 May 27 00:01 alpha-114.tar.asc   
-rw-r--r--   1 ftpuser ftponly  50559368 May 27 00:04 beta-211.tar.asc
-rw-r--r--   1 ftpuser ftponly  61320807 May 27 00:06 gamma-387.tar.asc
-rw-r--r--   1 ftpuser ftponly  43125044 May 27 00:07 epsilon-241.tar.asc
-rw-r--r--   1 ftpuser ftponly  1107110560 Apr 26 04:33 zeta-7728.tar.asc
-rw-r--r--   1 ftpuser ftponly  1107136555 Apr 27 00:29 zeta-7729.tar.asc
-rw-r--r--   1 ftpuser ftponly  1107154163 Apr 28 00:29 zeta-7731.tar.asc
-rw-r--r--   1 ftpuser ftponly  1107194763 May  1 00:33 zeta-7734.tar.asc
-rw-r--r--   1 ftpuser ftponly  1107200582 May  2 00:33 zeta-7736.tar.asc
-rw-r--r--   1 ftpuser ftponly  1107203436 May  4 00:32 zeta-7737.tar.asc
-rw-r--r--   1 ftpuser ftponly  1107250397 May  7 00:33 zeta-7739.tar.asc
-rw-r--r--   1 ftpuser ftponly  1107269251 May  8 00:26 zeta-7741.tar.asc
-rw-r--r--   1 ftpuser ftponly  1107917088 May  9 00:32 zeta-7747.tar.asc
-rw-r--r--   1 ftpuser ftponly  1107914021 May 10 00:22 zeta-7748.tar.asc
-rw-r--r--   1 ftpuser ftponly  1113095971 May 11 00:32 zeta-7751.tar.asc
-rw-r--r--   1 ftpuser ftponly  1114420811 May 12 00:32 zeta-7756.tar.asc
-rw-r--r--   1 ftpuser ftponly  1114433146 May 13 00:31 zeta-7757.tar.asc
-rw-r--r--   1 ftpuser ftponly  1114437345 May 14 00:32 zeta-7758.tar.asc
-rw-r--r--   1 ftpuser ftponly  1114437862 May 16 00:29 zeta-7762.tar.asc
-rw-r--r--   1 ftpuser ftponly  1115062371 May 17 00:29 zeta-7778.tar.asc
-rw-r--r--   1 ftpuser ftponly  1115068367 May 18 00:30 zeta-7781.tar.asc
-rw-r--r--   1 ftpuser ftponly  1115067272 May 19 00:24 zeta-7782.tar.asc
-rw-r--r--   1 ftpuser ftponly  1115077719 May 20 00:26 zeta-7784.tar.asc
-rw-r--r--   1 ftpuser ftponly  1115080120 May 22 00:25 zeta-7785.tar.asc
-rw-r--r--   1 ftpuser ftponly  1115076554 May 23 00:33 zeta-7786.tar.asc
-rw-r--r--   1 ftpuser ftponly  1115877146 May 24 00:26 zeta-7789.tar.asc
-rw-r--r--   1 ftpuser ftponly  1115967469 May 27 00:53 zeta-7795.tar.asc

I want to delete all but the most recent 3 of every file-prefix, so in this case all the zeta-**.tar.asc files except the last three. I have a kind of complicated perl script that accomplishes this, but I'd like do be able to do it with bash.

Slick snippets appreciated!

link|improve this question

If it is complicated in Perl, it will be even more complicated in bash. – SvenW May 27 '11 at 10:24
feedback

4 Answers

up vote 1 down vote accepted

This should work for an arbitrary amount of prefixes, as long as they follow the prefix- name convention:

for i in `ls | sed 's/-.*//' | uniq`; do ls -t $i-* | awk '{if(NR>3) print}' | xargs rm -f; done

link|improve this answer
feedback
ls -lt prefix*suffix | gawk ' { if ($NR > 3) print $NF } ' | xargs rm

Works for me. Isn't foolproof. No warranty. Your milleage may vary.

link|improve this answer
...see my previous comment... – Frank Brenner May 30 '11 at 10:41
feedback

You can use tail -n +3 to give you all bar the initial 3 lines of output from ls -1t e.g.

ls -1t zeta* | tail -n +3 | xargs rm 

This lists all the files from newest to oldest in a single column which head then processes and outputs a list of files after the initial 3 (newest) files.

link|improve this answer
Yeah, but then I have to pass each prefix explicitly. – Frank Brenner May 30 '11 at 10:40
feedback

This will delete all file older than 3 days. You can change it around. This basically looks for files whose modify time is less than now()-3days and deletes them.

find $PATH  -mtime -3 -type f -exec rm {}

You can optionally define a "-iname" flag to look for a specific pattern in the filenames

-iname suffix*prefix

For more options refer to the "find manpages"

link|improve this answer
Thanks, but that will delete all files if they get old enough. – Frank Brenner May 30 '11 at 10:42
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.