How to scan the contents of an entire directory, including its contents in subdirectories, and find the newest .pl file in it using Perl?
I want to create a sorted array / list of full file paths of all .pl files in a directory tree.
So, for example, if my base directory is /home/users/cheeseconqueso/ , I want to search for .pl files in this directory and any subdirectory in this path, and then sort the .pl files by date.
The end result will be an array, @pl_paths , where $pl_paths[0] will be something like /home/users/cheeseconqueso/maybe_not_newest_directory/surely_newest_file.pl
From this result, I want to execute the file, but I think as soon as I get the sorted array by executing the file in $pl_paths[0] , there will be no problem.
There is the same question that I tried to change according to my needs, but I'm here for obvious reasons.
The code I use to get the newest NAME file in only one directory:
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";
source share