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 $fixtopdir =~ s/\\dir$// if $Is_NT;
99 &finddir($wanted,$fixtopdir,$topnlink);
103 warn "Can't cd to $topdir: $!\n";
107 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
108 ($dir,$_) = ('.', $topdir);
111 chdir $dir && &$wanted;
120 ($wanted, $dir, $nlink) = @_;
122 my($dev, $ino, $mode, $subcount);
124 # Get the list of files in the current directory.
125 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
126 my(@filenames) = readdir(DIR);
129 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
138 else { # This dir has subdirectories.
139 $subcount = $nlink - 2;
146 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
148 # Get link count and check for directoriness.
150 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
151 # unless ($nlink || $dont_use_nlink);
155 # It really is a directory, so do it recursively.
157 if (!$prune && chdir $_) {
158 $name =~ s/\.dir$// if $Is_VMS;
159 $name =~ s/\\dir$// if $Is_NT;
160 &finddir($wanted,$name,$nlink);
174 $cwd = Cwd::fastcwd();;
176 # Localize these rather than lexicalizing them for backwards
178 local($topdir, $topdev, $topino, $topmode, $topnlink);
179 foreach $topdir (@_) {
180 (($topdev,$topino,$topmode,$topnlink) =
181 ($Is_VMS ? stat($topdir) : lstat($topdir)))
182 || (warn("Can't stat $topdir: $!\n"), next);
184 if (chdir($topdir)) {
185 my $fixtopdir = $topdir;
186 $fixtopdir =~ s,/$,, ;
187 $fixtopdir =~ s/\.dir$// if $Is_VMS;
188 $fixtopdir =~ s/\\dir$// if $Is_NT;
189 &finddepthdir($wanted,$fixtopdir,$topnlink);
190 ($dir,$_) = ($fixtopdir,'.');
195 warn "Can't cd to $topdir: $!\n";
199 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
200 ($dir,$_) = ('.', $topdir);
203 chdir $dir && &$wanted;
212 ($wanted,$dir,$nlink) = @_;
213 my($dev, $ino, $mode, $subcount);
215 # Get the list of files in the current directory.
216 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
217 my(@filenames) = readdir(DIR);
220 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
229 else { # This dir has subdirectories.
230 $subcount = $nlink - 2;
236 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
238 # Get link count and check for directoriness.
240 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
244 # It really is a directory, so do it recursively.
247 $name =~ s/\.dir$// if $Is_VMS;
248 $name =~ s/\\dir$// if $Is_NT;
249 &finddepthdir($wanted,$name,$nlink);
260 # Set dont_use_nlink in your hint file if your system's stat doesn't
261 # report the number of links in a directory as an indication
262 # of the number of files.
263 # See, e.g. hints/machten.sh for MachTen 2.2.
264 $dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
266 # These are hard-coded for now, but may move to hint files.
271 if ($^O =~ m:^mswin32:i) {
277 if $^O eq 'os2' || $^O eq 'dos' || $^O eq 'amigaos';