【发布时间】:2022-01-18 22:11:41
【问题描述】:
这个问题是一个特例 “在 perl 中,查找在给定时间间隔内修改的文件的最简单方法是什么?” 并且下面的解决方案可以很容易地推广。 在 bash 中,我们可以输入
> find . -mtime -1h
但我想在纯 perl 中执行此操作。下面的代码就是这样做的。它在每个文件上显式运行stat。
有没有什么方法可以让它变得更简单或更优雅?当然,这比 bash 命令慢。我并不是想与 bash 竞争效率。我只是想纯粹用 perl 来做。
#!/usr/bin/env perl
use strict; use warnings;
use File::Find;
my $invocation_seconds=time;
my $interval_left = $invocation_seconds - (60 * 60); # one hour ago
my $count_all=0;
my @selected;
find(
sub
{
$count_all++;
my $mtime_seconds=(stat($_))[9];
return unless defined $mtime_seconds; # if we edit files while running current script, this can be undef on occasion
return unless ($mtime_seconds>$interval_left);
push@selected,$File::Find::name;
}
,
'.', # current directory
);
my $end_seconds=time;
my $totalselected=scalar@selected;
print ($_,"\n",)for@selected;
print $^V; print " <- perl version\n";
print 'selected ',$totalselected, '/',$count_all,' in ',($end_seconds-$invocation_seconds),' seconds',"\n";
【问题讨论】: