开发者

How can I get all files in a directory, but not in subdirectories, in Perl?

开发者 https://www.devze.com 2022-12-12 19:38 出处:网络
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?

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 ".");

0

精彩评论

暂无评论...
验证码 换一张
取 消