如何在目录及其所有子目录中使用Perl找到最新的.pl文件? [英] How can I find the newest .pl file in a directory and all its subdirectories using Perl?
问题描述
$ b $如何扫描整个目录的内容,包括其子目录内容,并使用Perl查找最新的 .pl
文件?
我想在目录树中构建所有 .pl
文件的完整文件路径的排序数组/列表。
所以,例如,如果我的基本目录是 / home / users / cheeseconqueso /
我想搜索 .pl
该目录中的文件和该路径中的任何子目录,然后按日期排序 .pl
文件。
最终结果将是一个数组, @pl_paths
,其中 $ pl_paths [0]
会像 /home/users/cheeseconqueso/maybe_not_newest_directory/surely_newest_file.pl
这样的结果,我想要执行文件,但是我想一旦我得到排序的数组,在 $ pl_paths [0]
中执行该文件将不会是一个问题。 p>
有一个相似关于SO的问题,我一直在努力修改以适应我的需要但是我现在在这里有明显的原因。
我用于在一个目录中获取最新文件NAME的代码是:
opendir(我的$ DH,$ DIR)或死打开$ DIR:$!
我的%files = map {$ _ => (stat($ DIR / $ _))[9]} grep(!/^\.\.?$/,readdir($ DH));
closedir($ DH);
我的@sorted_files = sort {$ files {$ b}< => $ files {$ a}}(键%文件);
print $ sorted_files [0]。\\\
;
您可以使用文件:找到,如果你想要一个核心模块,但我宁愿使用文件:Find::Rule 。
要开始,我们可以找到
使用文件的目录下的所有 .pl
::查找::规则;
my @files = File :: Find :: Rule-> file
- > name('*。pl')
- > in($ directory);
然后让我们使用 map
将文件名与修改次数:
我的@files_with_mtimes = map + {name => $ _,mtime => (stat $ _)[9]},@files;
并按mtime排序:
my @sorted_files = reverse sort {$ a-> {mtime}< => $ b-> {mtime}}
@files_with_mtimes;
从那里,最新的名称在 $ sorted_files [ 0] {name}
。
如果你只想找到顶级的,实际上不需要做一个完整的排序,但是最好的解决方案我可以想到涉及一些稍微高级的FP,所以如果看起来很奇怪,不要担心:
使用List :: Util'reduce';
我的($ top_file)= reduce {$ a-> {mtime}> = $ b-> {mtime}? $ a:$ b}
@files_with_mtimes;
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";
You can use File::Find if you want a core module for this, but I would prefer to use File::Find::Rule.
To start off, we can find all of the .pl
files under a directory with
use File::Find::Rule;
my @files = File::Find::Rule->file
->name('*.pl')
->in($directory);
Then let's use map
to associate filenames with their modification times:
my @files_with_mtimes = map +{ name => $_, mtime => (stat $_)[9] }, @files;
And sort them by mtime:
my @sorted_files = reverse sort { $a->{mtime} <=> $b->{mtime} }
@files_with_mtimes;
And from there, the name of the newest one is in $sorted_files[0]{name}
.
If you only want to find the top one, there's actually no need to do a complete sort, but the nicest solution I can think of involves some slightly advanced FP, so don't worry about it at all if it looks strange to you:
use List::Util 'reduce';
my ($top_file) = reduce { $a->{mtime} >= $b->{mtime} ? $a : $b }
@files_with_mtimes;
这篇关于如何在目录及其所有子目录中使用Perl找到最新的.pl文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!