1

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!

user9517
  • 115,471
  • 20
  • 215
  • 297
Frank Brenner
  • 175
  • 5
  • 11

4 Answers4

2

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.

user9517
  • 115,471
  • 20
  • 215
  • 297
1
ls -lt prefix*suffix | gawk ' { if ($NR > 3) print $NF } ' | xargs rm

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

Paweł Brodacki
  • 6,511
  • 20
  • 23
1

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

opsguy
  • 801
  • 1
  • 5
  • 12
0

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"

user9517
  • 115,471
  • 20
  • 215
  • 297
Sameer
  • 4,118
  • 2
  • 17
  • 11