Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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 exploiting the Monastery: (7)
As of 2015-07-04 11:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (59 votes), past polls