File::Find
use File::Find;
# Print out all directories below current one.
find sub { print "$File::Find::name\n" if -d }, ".";
# Compute total space used by all files in listed directories.
@dirs = @ARGV ? @ARGV : ('.');
my $sum = 0;
find sub { $sum += -s }, @dirs;
print "@dirs contained $sum bytes\n";
# Alter default behavior to go through symlinks
# and visit subdirectories first.
find { wanted => \&myfunc, follow => 1, bydepth => 1 }, ".";The File::Find module's
find function recursively descends directories. Its
first argument should be a reference to a function, and all following
arguments should be directories. The function is called on each
filename from the listed directories. Within that function, the
$_ variable is set to the basename of the current
filename visited, and the process's current working directory is by
default set to that directory. The package variable
$File::Find::name is the full pathname of the
visited filename. An alternative calling convention takes as its first
argument a reference to a hash containing option specifications,
including "wanted", "bydepth",
"follow", "follow_fast",
"follow_skip", "no_chdir",
"untaint", "untaint_pattern",
and "untaint_skip", as fully explained in the
online documentation. This module is also used by the standard
find2perl (1) translator program that
comes with Perl.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access