How can I find all the files that match a certain criteria (-M
, modification age in days) in a list of directories, but not in th开发者_如何学Goeir subdirectories?
I wanted to use File::Find, but looks like it always goes to the subdirectories too.
@files = grep { -f && (-M) < 5 } <$_/*> for @folders;
Use readdir or File::Slurp::read_dir in conjunction with grep.
#!/usr/bin/perl
use strict;
use warnings;
use File::Slurp;
use File::Spec::Functions qw( canonpath catfile );
my @dirs = (@ENV{qw(HOME TEMP)});
for my $dir ( @dirs ) {
print "'$dir'\n";
my @files = grep { 2 > -M and -f }
map { canonpath(catfile $dir, $_) } read_dir $dir;
print "$_\n" for @files;
}
You can set File::Find::prune within the 'wanted' function to skip directory trees. Add something like $File::Find::prune = 1 if( -d && $File::Find::name ne ".");
精彩评论