#!/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] : () } ; 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 #### use strict; my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { chomp( my $x = $_ ); s!\D+!!g; [ $x, $_ ] } ; # 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