8 find - traverse a file tree
10 finddepth - traverse a directory structure depth-first
15 find(\&wanted, '/foo','/bar');
19 finddepth(\&wanted, '/foo','/bar');
24 The first argument to find() is either a hash reference describing the
25 operations to be performed for each file, or a code reference. If it
26 is a hash reference, then the value for the key C<wanted> should be a
27 code reference. This code reference is called I<the wanted()
30 Currently the only other supported key for the above hash is
31 C<bydepth>, in presense of which the walk over directories is
32 performed depth-first. Entry point finddepth() is a shortcut for
33 specifying C<{ bydepth => 1}> in the first argument of find().
35 The wanted() function does whatever verifications you want.
36 $File::Find::dir contains the current directory name, and $_ the
37 current filename within that directory. $File::Find::name contains
38 C<"$File::Find::dir/$_">. You are chdir()'d to $File::Find::dir when
39 the function is called. The function may set $File::Find::prune to
42 File::Find assumes that you don't alter the $_ variable. If you do then
43 make sure you return it to its original value before exiting your function.
45 This library is useful for the C<find2perl> tool, which when fed,
47 find2perl / -name .nfs\* -mtime +7 \
48 -exec rm -f {} \; -o -fstype nfs -prune
50 produces something like:
54 (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
58 ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
60 ($File::Find::prune = 1);
63 Set the variable $File::Find::dont_use_nlink if you're using AFS,
66 C<finddepth> is just like C<find>, except that it does a depth-first
69 Here's another interesting wanted function. It will find all symlinks
73 -l && !-e && print "bogus link: $File::Find::name\n";
78 There is no way to make find or finddepth follow symlinks.
83 @EXPORT = qw(find finddepth);
88 my $bydepth = $wanted->{bydepth};
89 my $cwd = $bydepth ? Cwd::fastcwd() : Cwd::cwd();
90 # Localize these rather than lexicalizing them for backwards
92 local($topdir,$topdev,$topino,$topmode,$topnlink);
93 foreach $topdir (@_) {
94 (($topdev,$topino,$topmode,$topnlink) =
95 ($Is_VMS ? stat($topdir) : lstat($topdir)))
96 || (warn("Can't stat $topdir: $!\n"), next);
101 ($dir,$_) = ($topdir,'.');
103 $wanted->{wanted}->();
106 my $fixtopdir = $topdir;
107 $fixtopdir =~ s,/$,, ;
108 $fixtopdir =~ s/\.dir$// if $Is_VMS;
109 &finddir($wanted,$fixtopdir,$topnlink, $bydepth);
111 ($dir,$_) = ($fixtopdir,'.');
113 $wanted->{wanted}->();
117 warn "Can't cd to $topdir: $!\n";
121 require File::Basename;
122 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
123 ($dir,$_) = ('.', $topdir);
127 $wanted->{wanted}->();
130 warn "Can't cd to $dir: $!\n";
138 my($wanted, $nlink, $bydepth);
140 ($wanted, $dir, $nlink, $bydepth) = @_;
142 my($dev, $ino, $mode, $subcount);
144 # Get the list of files in the current directory.
145 opendir(DIR,'.') || (warn("Can't open $dir: $!\n"), $bydepth || return);
146 my(@filenames) = readdir(DIR);
149 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
155 $wanted->{wanted}->();
158 else { # This dir has subdirectories.
159 $subcount = $nlink - 2;
164 $prune = 0 unless $bydepth;
166 $wanted->{wanted}->() unless $bydepth;
167 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
169 # Get link count and check for directoriness.
171 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
172 # unless ($nlink || $dont_use_nlink);
176 # It really is a directory, so do it recursively.
181 $name =~ s/\.dir$// if $Is_VMS;
182 &finddir($wanted,$name,$nlink, $bydepth);
186 warn "Can't cd to $_: $!\n";
190 $wanted->{wanted}->() if $bydepth;
197 defined &$wanted ? {wanted => $wanted} : $wanted;
202 find_opt(wrap_wanted($wanted), @_);
206 my $wanted = wrap_wanted(shift);
207 $wanted->{bydepth} = 1;
208 find_opt($wanted, @_);
211 # These are hard-coded for now, but may move to hint files.
218 if $^O eq 'os2' || $^O eq 'dos' || $^O eq 'amigaos' || $^O eq 'MSWin32';
220 # Set dont_use_nlink in your hint file if your system's stat doesn't
221 # report the number of links in a directory as an indication
222 # of the number of files.
223 # See, e.g. hints/machten.sh for MachTen 2.2.
224 unless ($dont_use_nlink) {
226 $dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});