How can I scan an entire directory's contents, including its subdirectories' contents, and find the newest .pl
file within them using Perl?
I want to build a sorted array/list of the full file paths of all .pl
files within a directory tree.
So, for example, if my base directory is /home/users/cheeseconqueso/
I want to search for .pl
files in that directory and any subdirectory within that path and then sort the .pl
files by date.
The end result would be an array, @pl_paths
, where $pl_paths[0]
would be something like /home/users/cheeseconqueso/maybe_not_newest_directory/surely_newest_file.pl
From that result, I want to execute the file, but I think once I get the sorted array figured out, executing the file in $pl_paths[0]
, won't be a problem.
There is a similar question on SO that I have been trying to modify to suit my needs, but I am here now for obvious reasons.
The code I'm using to get the newest file NAME only in one directory is:
opendir(my $DH, $DIR) or die "Error opening $DIR: $!";
my %files = map { $_ => (stat("$DIR/$_"))[9] } grep(! /^\.\.?$/, readdir($DH));
closedir($DH);
my @sorted_files = sort { $files{$b} <=> $files{$a} } (keys %files);
print $sorted_files[0]."\n";