Perlfan.Felix has asked for the wisdom of the Perl Monks concerning the following question:

Hi, is there a 'glob' for directories in perl ?

I want to find a list of directories matching a pattern.
For example, $pattern is set to tomcat_????_?? and $directory is set to /var/log/tomcat.
Now I want to find these subdirectories:

/var/log/tomcat/tomcat_2016_09/
/var/log/tomcat/tomcat_2016_10/

  • Comment on Getting a list of directories matching a pattern

Replies are listed 'Best First'.
Re: Getting a list of directories matching a pattern
by Marshall (Canon) on Jul 21, 2016 at 22:54 UTC
    You can use glob to get all files (a directory is just special kind of file), then use grep to filter out the plain files. To get directories underneath the currently running program...
    #!/usr/bin/perl use warnings; use strict; my @dirs = grep{-d $_}glob "*"; print join "\n",@dirs;
    Make sure you take into account the path if not in the current directory.
Re: Getting a list of directories matching a pattern (glob File::Find::Rule)
by Anonymous Monk on Jul 21, 2016 at 22:29 UTC
    Just use glob? or
    use File::Find::Rule qw/ find rule / ; my @dirs = find( directory => maxdepth => 1, name => qr/tomcat_\d\d\d\d_\d\d/, )->in($startdir);