find - traverse a file tree |
find - traverse a file tree
finddepth - traverse a directory structure depth-first
use File::Find; find(\&wanted, '/foo', '/bar'); sub wanted { ... }
use File::Find; finddepth(\&wanted, '/foo', '/bar'); sub wanted { ... }
use File::Find; find({ wanted => \&process, follow => 1 }, '.');
The first argument to find()
is either a hash reference describing the
operations to be performed for each file, or a code reference.
Here are the possible keys for the hash:
wanted
bydepth
finddepth()
is a shortcut for
specifying { bydepth =
1 }> in the first argument of find().
preprocess
readdir()
but before the loop that
calls the wanted()
function. It is called with a list of strings and is
expected to return a list of strings. The code can be used to sort the
strings alphabetically, numerically, or to filter out directory entries based
on their name alone.
postprocess
follow
$File::Find::fullname
which holds the absolute
pathname of the file with all symbolic links resolved
follow_fast
follow_skip
follow_skip==1
, which is the default, causes all files which are
neither directories nor symbolic links to be ignored if they are about
to be processed a second time. If a directory or a symbolic link
are about to be processed a second time, File::Find dies.
follow_skip==0
causes File::Find to die if any file is about to be
processed a second time.
follow_skip==2
causes File::Find to ignore any duplicate files and
dirctories but to proceed normally otherwise.
no_chdir
chdir()
to each directory as it recurses. The wanted()
function will need to be aware of this, of course. In this case,
$_
will be the same as $File::Find::name
.
untaint
untaint_pattern
qr
quoting operator.
The default is set to qr|^([-+@\w./]+)$|
.
Note that the parantheses are vital.
untaint_skip
The wanted()
function does whatever verifications you want.
$File::Find::dir
contains the current directory name, and $_
the
current filename within that directory. $File::Find::name
contains
the complete pathname to the file. You are chdir()'d to
$File::Find::dir
when the function is called, unless no_chdir
was specified. When <follow> or <follow_fast> are in effect, there is
also a $File::Find::fullname
. The function may set
$File::Find::prune
to prune the tree unless bydepth
was
specified. Unless follow
or follow_fast
is specified, for
compatibility reasons (find.pl, find2perl) there are in addition the
following globals available: $File::Find::topdir
,
$File::Find::topdev
, $File::Find::topino
,
$File::Find::topmode
and $File::Find::topnlink
.
This library is useful for the find2perl
tool, which when fed,
find2perl / -name .nfs\* -mtime +7 \ -exec rm -f {} \; -o -fstype nfs -prune
produces something like:
sub wanted { /^\.nfs.*\z/s && (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_)) && int(-M _) > 7 && unlink($_) || ($nlink || (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_))) && $dev < 0 && ($File::Find::prune = 1); }
Set the variable $File::Find::dont_use_nlink
if you're using AFS,
since AFS cheats.
Here's another interesting wanted function. It will find all symlinks that don't resolve:
sub wanted { -l && !-e && print "bogus link: $File::Find::name\n"; }
See also the script pfind
on CPAN for a nice application of this
module.
Be aware that the option to follow symbolic links can be dangerous.
Depending on the structure of the directory tree (including symbolic
links to directories) you might traverse a given (physical) directory
more than once (only if follow_fast
is in effect).
Furthermore, deleting or changing files in a symbolically linked directory
might cause very unpleasant surprises, since you delete or change files
in an unknown directory.
find - traverse a file tree |