Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
laziness, impatience, and hubris
 
PerlMonks  

Re: find the latest created file in a directory

by johngg (Abbot)
on Apr 24, 2013 at 20:45 UTC ( #1030524=note: print w/ replies, xml ) Need Help??


in reply to find the latest created file in a directory

Using &List::Util::reduce, can save having to sort.

$ perl -MList::Util=reduce -Mstrict -Mwarnings -E ' my $newest = reduce { $a->[ 1 ] > $b->[ 1 ] ? $a : $b } do { opendir my $dotDH, q{.} or die qq{opendir: .: $!\n}; map { [ $_, ( stat $_ )[ 9 ] ] } grep { -f and not -l } readdir $dotDH; }; say qq{Newest file - $newest->[ 0 ]};' Newest file - spw1029421 $

And that really is the newest file in the directory :-)

Cheers,

JohnGG


Comment on Re: find the latest created file in a directory
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1030524]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-04-20 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls