6 require File::Basename;
11 find - traverse a file tree
13 finddepth - traverse a directory structure depth-first
18 find(\&wanted, '/foo','/bar');
22 finddepth(\&wanted, '/foo','/bar');
27 The wanted() function does whatever verifications you want.
28 $File::Find::dir contains the current directory name, and $_ the
29 current filename within that directory. $File::Find::name contains
30 C<"$File::Find::dir/$_">. You are chdir()'d to $File::Find::dir when
31 the function is called. The function may set $File::Find::prune to
34 File::Find assumes that you don't alter the $_ variable. If you do then
35 make sure you return it to its original value before exiting your function.
37 This library is primarily for the C<find2perl> tool, which when fed,
39 find2perl / -name .nfs\* -mtime +7 \
40 -exec rm -f {} \; -o -fstype nfs -prune
42 produces something like:
46 (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
50 ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
52 ($File::Find::prune = 1);
55 Set the variable $File::Find::dont_use_nlink if you're using AFS,
58 C<finddepth> is just like C<find>, except that it does a depth-first
61 Here's another interesting wanted function. It will find all symlinks
65 -l && !-e && print "bogus link: $File::Find::name\n";
70 There is no way to make find or finddepth follow symlinks.
75 @EXPORT = qw(find finddepth);
81 # Localize these rather than lexicalizing them for backwards
83 local($topdir,$topdev,$topino,$topmode,$topnlink);
84 foreach $topdir (@_) {
85 (($topdev,$topino,$topmode,$topnlink) =
86 ($Is_VMS ? stat($topdir) : lstat($topdir)))
87 || (warn("Can't stat $topdir: $!\n"), next);
90 ($dir,$_) = ($topdir,'.');
95 my $fixtopdir = $topdir;
96 $fixtopdir =~ s,/$,, ;
97 $fixtopdir =~ s/\.dir$// if $Is_VMS;
98 &finddir($wanted,$fixtopdir,$topnlink);
101 warn "Can't cd to $topdir: $!\n";
105 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
106 ($dir,$_) = ('.', $topdir);
113 warn "Can't cd to $dir: $!\n";
123 ($wanted, $dir, $nlink) = @_;
125 my($dev, $ino, $mode, $subcount);
127 # Get the list of files in the current directory.
128 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
129 my(@filenames) = readdir(DIR);
132 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
141 else { # This dir has subdirectories.
142 $subcount = $nlink - 2;
149 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
151 # Get link count and check for directoriness.
153 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
154 # unless ($nlink || $dont_use_nlink);
158 # It really is a directory, so do it recursively.
163 $name =~ s/\.dir$// if $Is_VMS;
164 &finddir($wanted,$name,$nlink);
168 warn "Can't cd to $_: $!\n";
179 my $cwd = Cwd::cwd();
180 # Localize these rather than lexicalizing them for backwards
182 local($topdir,$topdev,$topino,$topmode,$topnlink);
183 foreach $topdir (@_) {
184 (($topdev,$topino,$topmode,$topnlink) =
185 ($Is_VMS ? stat($topdir) : lstat($topdir)))
186 || (warn("Can't stat $topdir: $!\n"), next);
188 if (chdir($topdir)) {
189 my $fixtopdir = $topdir;
190 $fixtopdir =~ s,/$,, ;
191 $fixtopdir =~ s/\.dir$// if $Is_VMS;
192 &finddepthdir($wanted,$fixtopdir,$topnlink);
193 ($dir,$_) = ($topdir,'.');
198 warn "Can't cd to $topdir: $!\n";
202 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
203 ($dir,$_) = ('.', $topdir);
210 warn "Can't cd to $dir: $!\n";
220 ($wanted, $dir, $nlink) = @_;
221 my($dev, $ino, $mode, $subcount);
223 # Get the list of files in the current directory.
224 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
225 my(@filenames) = readdir(DIR);
228 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
237 else { # This dir has subdirectories.
238 $subcount = $nlink - 2;
244 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
246 # Get link count and check for directoriness.
248 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
252 # It really is a directory, so do it recursively.
256 $name =~ s/\.dir$// if $Is_VMS;
257 &finddepthdir($wanted,$name,$nlink);
261 warn "Can't cd to $_: $!\n";
270 # Set dont_use_nlink in your hint file if your system's stat doesn't
271 # report the number of links in a directory as an indication
272 # of the number of files.
273 # See, e.g. hints/machten.sh for MachTen 2.2.
274 $dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
276 # These are hard-coded for now, but may move to hint files.
281 if ($^O =~ m:^mswin32:i) {
287 if $^O eq 'os2' || $^O eq 'dos' || $^O eq 'amigaos';