I may have coded this wrong, but my sample data seems to work okay. This is probably highly inefficient, but it's fast enough for the job

#!/usr/bin/perl -w $| = 1; use strict; my @sorted = map { $_->[0] } sort { ($a->[1] <=> $b->[1]) or ($a->[2] <=> $b->[2]) or ($a->[3] <=> $b->[3]) or ($a->[4] <=> $b->[4]) or ($a->[5] <=> $b->[5]) or ($a->[6] <=> $b->[6]) } map { m!^(access_log_(\d+)_(\d+)_(\d+)_(\d+)_(\d+)_(\d+))$! ? [$1, $2, $3, $4, $5, $6, $7] : () } <DATA>; print "Listed from oldest to newest:\n\n", join("\n", @sorted), "\n"; __DATA__ access_log_04_05_20_03_00_00 access_log_04_05_20_03_30_00 access_log_04_05_20_03_30_01 access_log_04_05_20_03_29_30 access_log_04_05_19_03_30_00 access_log_04_05_17_03_30_00 access_log_04_04_20_03_30_00 access_log_04_05_20_03_31_00

Update: This one should be much much better. We can just strip everything but the digits and do a single comparision:

use strict; my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { chomp( my $x = $_ ); s!\D+!!g; [ $x, $_ ] } <DATA>; # for your case, we just pop() off the last element # (which would be the current log) and handle the rest! my $current = pop( @sorted ); warn( "$current is newest logfile, not touching it!\n" ); for my $log ( @sorted ) { # unlink( '/var/logs/' . $log; # or whatever } __DATA__ access_log_04_05_20_03_00_00 access_log_04_05_20_03_30_00 access_log_04_05_20_03_30_01 access_log_04_05_20_03_29_30 access_log_03_05_19_03_30_00 access_log_04_05_17_03_30_00 access_log_04_04_20_03_30_00 access_log_04_05_20_03_31_00

In reply to Re: ordring logrotate file names by saskaqueer
in thread ordring logrotate file names by Anonymous Monk

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.