tags:

views:

122

answers:

3

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 their subdirectories?

I wanted to use File::Find, but looks like it always goes to the subdirectories too.

+3  A: 

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;
}
Sinan Ünür
+9  A: 
@files = grep { -f && (-M) < 5 } <$_/*> for @folders;
Ed Guiness
when doing multiple -X's on the same file, you can use _ as the "filename" on all but the first to reuse the same stat return
ysth
A: 

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

William Pursell
If you don't want to do a depth first search, don't use a depth first search tool. :)
brian d foy