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);
102 warn "Can't cd to $topdir: $!\n";
106 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
107 ($dir,$_) = ('.', $topdir);
110 chdir $dir && &$wanted;
119 ($wanted, $dir, $nlink) = @_;
121 my($dev, $ino, $mode, $subcount);
123 # Get the list of files in the current directory.
124 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
125 my(@filenames) = readdir(DIR);
128 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
137 else { # This dir has subdirectories.
138 $subcount = $nlink - 2;
145 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
147 # Get link count and check for directoriness.
149 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
150 # unless ($nlink || $dont_use_nlink);
154 # It really is a directory, so do it recursively.
156 if (!$prune && chdir $_) {
157 $name =~ s/\.dir$// if $Is_VMS;
158 &finddir($wanted,$name,$nlink);
172 $cwd = Cwd::fastcwd();;
174 # Localize these rather than lexicalizing them for backwards
176 local($topdir, $topdev, $topino, $topmode, $topnlink);
177 foreach $topdir (@_) {
178 (($topdev,$topino,$topmode,$topnlink) =
179 ($Is_VMS ? stat($topdir) : lstat($topdir)))
180 || (warn("Can't stat $topdir: $!\n"), next);
182 if (chdir($topdir)) {
183 my $fixtopdir = $topdir;
184 $fixtopdir =~ s,/$,, ;
185 $fixtopdir =~ s/\.dir$// if $Is_VMS;
186 &finddepthdir($wanted,$fixtopdir,$topnlink);
187 ($dir,$_) = ($fixtopdir,'.');
192 warn "Can't cd to $topdir: $!\n";
196 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
197 ($dir,$_) = ('.', $topdir);
200 chdir $dir && &$wanted;
209 ($wanted,$dir,$nlink) = @_;
210 my($dev, $ino, $mode, $subcount);
212 # Get the list of files in the current directory.
213 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
214 my(@filenames) = readdir(DIR);
217 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
226 else { # This dir has subdirectories.
227 $subcount = $nlink - 2;
233 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
235 # Get link count and check for directoriness.
237 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
241 # It really is a directory, so do it recursively.
244 $name =~ s/\.dir$// if $Is_VMS;
245 &finddepthdir($wanted,$name,$nlink);
256 # Set dont_use_nlink in your hint file if your system's stat doesn't
257 # report the number of links in a directory as an indication
258 # of the number of files.
259 # See, e.g. hints/machten.sh for MachTen 2.2.
260 $dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
262 # These are hard-coded for now, but may move to hint files.
267 if ($^O =~ m:^mswin32:i) {
273 if $^O eq 'os2' || $^O eq 'dos' || $^O eq 'amigaos';