0

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.

simbabque
  • 53,749
  • 8
  • 73
  • 136
veerabhadra
  • 21
  • 1
  • 5

1 Answers1

7

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*"));
ikegami
  • 367,544
  • 15
  • 269
  • 518