If I'm understanding you correctly, perhaps the following will be helpful:

use strict; use warnings; chomp( my @fileNames = <DATA> ); my @sortedFileNames = map $_->[0], sort { $b->[1] <=> $a->[1] } map { my ( $d, $m, $y ) = /(\d+)/g; [ $_, "$y$m$d" ] } grep /^backup_\d\d_\d\d_\d{4}.bak$/, @fileNames; shift @sortedFileNames; if (@sortedFileNames) { print "$_\n" for @sortedFileNames; #unlink @sortedFileNames; } __DATA__ backup_21_01_2013.bak file.txt backup_20_01_2013.bak what_is_this.doc backup_24_01_2013.bak never_open_this.docx backup_22_01_2013.bak stuff.ini backup_23_01_2013.bak more_stuff.ini deleteOldBackups.pl

Output (the files that would be deleted):

backup_23_01_2013.bak backup_22_01_2013.bak backup_21_01_2013.bak backup_20_01_2013.bak

If you populate @fileNames with the file names in the directory where the backups live, it will grep them only allowing backup-patterned files through. Then, using a Schwartzian transform, it sorts the backup file names in decending order and shifts off the first element (most recent backup file name) from @sortedFileNames. As it is now, the file names in @sortedFileNames are printed, but the unlink line can be uncommented, so all but the most recent backup files are deleted.

** Please thoroughly test and verify this on a copy of the backup directory before going live with it. **


In reply to Re: Delete all but the most recent backup file by Kenosis
in thread Delete all but the most recent backup file by jagexCoder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.