4

I have a set of directories in an array as

chdir /tmp;
my @dir=("test" "abc" "def")

I am looking for a way using File::Find::Rule to find all files in /tmp recursively, but excluding files from the ones in @dir.

I could get all files from @dir by

my $rule =  File::Find::Rule->new;
$rule->file;
my @files = $rule->in( @dir );

But I cannot negate the condition, that is to exclude @dir. I was trying

chdir /tmp;
@files = File::Find::Rule->new
    ->file()
    ->name(@dir)
    ->prune
    ->in( "." );

but no output.

nohup
  • 3,105
  • 3
  • 27
  • 52

1 Answers1

7

The documentation lists this example:

ignore CVS directories

my $rule = File::Find::Rule->new; $rule->or($rule->new
               ->directory
               ->name('CVS')
               ->prune
               ->discard,
          $rule->new);

Note here the use of a null rule. Null rules match anything they see, so the effect is to match (and discard) directories called 'CVS' or to match anything

You could probalby do the same thing:

my @exclude_dirs = qw(test abc def);
my $rule = File::Find::Rule->new; 
$rule->or($rule->new
               ->directory
               ->name(@exclude_dirs)
               ->prune
               ->discard,
          $rule->new);
my @files = $rule->in('/tmp');

Consider this example:

foo@bar:~/temp/filefind> tree
.
├── bar
│   ├── bar.txt
│   └── foobar.txt
├── baz.txt
└── foo
    └── foo.txt

2 directories, 4 files

Here's the code:

#!/usr/bin/perl
use strictures;
use File::Find::Rule;
use Data::Dump;

my @exclude_dirs = qw(foo);
my $rule = File::Find::Rule->new; 
$rule->or($rule->new
               ->directory
               ->name(@exclude_dirs)
               ->prune
               ->discard,
          $rule->new);
my @files = $rule->in('filefind');
dd \@files;

And now I run this:

foo@bar:~/temp> perl file-find.pl
[
  "filefind",
  "filefind/baz.txt",
  "filefind/bar",
  "filefind/bar/bar.txt",
  "filefind/bar/foobar.txt",
]
simbabque
  • 53,749
  • 8
  • 73
  • 136
  • I tried the same initially, after refering the documentation, but that was giving empty output as well. Tried the list of directories as an array, and as a space separated string, but both didn't work. – nohup Jan 29 '14 at 12:58
  • Just tried it. It's working for me with folders in my file system. – simbabque Jan 29 '14 at 13:01
  • 1
    Got the error. I re-defined a my variable inside that subroutine, which I declared and used before. Thank you simbabque. – nohup Jan 29 '14 at 13:11