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";
71 @EXPORT = qw(find finddepth);
77 # Localize these rather than lexicalizing them for backwards
79 local($topdir,$topdev,$topino,$topmode,$topnlink);
80 foreach $topdir (@_) {
81 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
82 || (warn("Can't stat $topdir: $!\n"), next);
85 ($dir,$_) = ($topdir,'.');
88 my $fixtopdir = $topdir;
89 $fixtopdir =~ s,/$,, ;
90 $fixtopdir =~ s/\.dir$// if $Is_VMS;
91 $fixtopdir =~ s/\\dir$// if $Is_NT;
92 &finddir($wanted,$fixtopdir,$topnlink);
95 warn "Can't cd to $topdir: $!\n";
99 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
100 ($dir,$_) = ('.', $topdir);
103 chdir $dir && &$wanted;
112 ($wanted, $dir, $nlink) = @_;
114 my($dev, $ino, $mode, $subcount);
116 # Get the list of files in the current directory.
117 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
118 my(@filenames) = readdir(DIR);
121 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
130 else { # This dir has subdirectories.
131 $subcount = $nlink - 2;
138 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
140 # Get link count and check for directoriness.
142 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
143 # unless ($nlink || $dont_use_nlink);
147 # It really is a directory, so do it recursively.
149 if (!$prune && chdir $_) {
150 $name =~ s/\.dir$// if $Is_VMS;
151 $name =~ s/\\dir$// if $Is_NT;
152 &finddir($wanted,$name,$nlink);
166 $cwd = Cwd::fastcwd();;
168 # Localize these rather than lexicalizing them for backwards
170 local($topdir, $topdev, $topino, $topmode, $topnlink);
171 foreach $topdir (@_) {
172 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
173 || (warn("Can't stat $topdir: $!\n"), next);
175 if (chdir($topdir)) {
176 my $fixtopdir = $topdir;
177 $fixtopdir =~ s,/$,, ;
178 $fixtopdir =~ s/\.dir$// if $Is_VMS;
179 $fixtopdir =~ s/\\dir$// if $Is_NT;
180 &finddepthdir($wanted,$fixtopdir,$topnlink);
181 ($dir,$_) = ($fixtopdir,'.');
186 warn "Can't cd to $topdir: $!\n";
190 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
191 ($dir,$_) = ('.', $topdir);
193 chdir $dir && &$wanted;
202 ($wanted,$dir,$nlink) = @_;
203 my($dev, $ino, $mode, $subcount);
205 # Get the list of files in the current directory.
206 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
207 my(@filenames) = readdir(DIR);
210 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
219 else { # This dir has subdirectories.
220 $subcount = $nlink - 2;
226 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
228 # Get link count and check for directoriness.
230 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
234 # It really is a directory, so do it recursively.
237 $name =~ s/\.dir$// if $Is_VMS;
238 $name =~ s/\\dir$// if $Is_NT;
239 &finddepthdir($wanted,$name,$nlink);
250 # Set dont_use_nlink in your hint file if your system's stat doesn't
251 # report the number of links in a directory as an indication
252 # of the number of files.
253 # See, e.g. hints/machten.sh for MachTen 2.2.
254 $dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
256 # These are hard-coded for now, but may move to hint files.
261 if ($^O =~ m:^mswin32:i) {
267 if $^O eq 'os2' || $^O eq 'msdos' || $^O eq 'amigaos';