veerabhadra
veerabhadra

Reputation: 21

Is it posible to use wild card to get list of directory in File::Find perl module?

I want to list the directory path which contains the required directory.

For example:

                                 /usr1
                                    |
                                    |
                -----------------------------------------   
                |                                       |
            /local1                                     /local2
                |                                       |
              dir1                                      dir1

I want to find the directory path where dir1 is present using wild card *.

From linux command line I can do this to get the result.

find /usr1/local* -name dir1 -type d

then it will shows

/usr1/local1/dir1
/usr1/local2/dir1

The same way how can I do with File::Find perl module.

I don't want to use system or `` to get it done.

Upvotes: 0

Views: 507

Answers (1)

ikegami
ikegami

Reputation: 386386

The equivalent of

find /usr1/local* ...

is

find(..., glob("/usr1/local*"))

so the whole is

use File::Basename qw( basename );
use File::Find     qw( find );

my $wanted = sub {
   say if basename($_) eq "dir1" && -d $_;
};

find({ wanted => $wanted, no_chdir => 1 }, glob("/usr1/local*"));

Personally, I prefer File::Find::Rule.

use File::Find::Rule qw( );

say
   for
      File::Find::Rule
         ->name('dir1')
         ->directory
         ->in(glob("/usr1/local*"));

Upvotes: 7

Related Questions