Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: find "x" quantity newest files in a dir

by mr.nick (Chaplain)
on Jun 27, 2001 at 19:55 UTC ( [id://91952] : note . print w/replies, xml ) Need Help??


in reply to find "x" quantity newest files in a dir

I'll add my belated $0.02 here ... if you want to grab the 10 most recent files in a directory, try something like:
my $dir="."; my @files=(map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [ -M $_, $_ ] } glob("$dir/*"))[0..9];
Then you can simply do whatever operation you want on @files. If you want all the files EXCEPT the newest ten, you can invert the sort logic and play with splice ala:
my $dir="."; my @files=(map { $_->[1] } sort { $b->[0] <=> $a->[0] } map { [ -M $_, $_ ] } glob("$dir/*")); splice (@files,-10);

mr.nick ...