Perl: Glob recursively
I wrote a simple function to let you glob a directory recursively. It's limited to a single path/glob pattern, but that's good enough for now.
use File::Find;
my @files = globr("/etc/*.cfg");
sub globr {
my ($str) = @_;
my ($dir,$glob) = $str =~ m/(.*\/)(.*)/;
$dir ||= './'; # Only a glob, so assume current dir
$glob ||= $str; # No dir only a glob: *.pl
# Find all the dirs in the target dir so we can recurse through them later
my (@ret, @dirs);
find( { wanted => sub { if (-d $_) { push(@dirs, $_) } }, no_chdir => 1 }, $dir);
# Go through each dir we found above and glob in them for matching files
foreach my $dir (@dirs) {
my @g = glob("$dir/$glob");
push(@ret, @g);
}
return @ret;
}
See also: Find files recursively