Is there an elegant way in Perl to find the newest file in a directory (newest by modification date)?
What I have so far is searching for the files I need, and for each one get it’s modification time, push into an array containing the filename, modification time, then sort it.
There must be a better way.
Your way is the ‘right’ way if you need a sorted list (and not just the first, see Brian’s answer for that). If you don’t fancy writing that code yourself, use this
Personally I wouldn’t go with the
ls -tmethod – that involves forking another program and it’s not portable. Hardly what I’d call ‘elegant’!Regarding rjray’s solution hand coded solution, I’d change it slightly:
After this,
@sorted_filescontains the sorted list, where the 0th element is the newest file, and each element itself contains a reference to the results ofstat, with the filename itself in the last element:The advantage of this is that it’s easier to change the sorting method later, if desired.
EDIT: here’s an easier-to-read (but longer) version of the directory scan, which also ensures that only plain files are added to the listing:
NB: the test for
defined()on the result ofreaddir()is because a file called ‘0’ would cause the loop to fail if you only test forif (my $file = readdir($DH))