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