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) =
82 ($Is_VMS ? stat($topdir) : lstat($topdir)))
83 || (warn("Can't stat $topdir: $!\n"), next);
86 ($dir,$_) = ($topdir,'.');
91 my $fixtopdir = $topdir;
92 $fixtopdir =~ s,/$,, ;
93 $fixtopdir =~ s/\.dir$// if $Is_VMS;
94 $fixtopdir =~ s/\\dir$// if $Is_NT;
95 &finddir($wanted,$fixtopdir,$topnlink);
99 warn "Can't cd to $topdir: $!\n";
103 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
104 ($dir,$_) = ('.', $topdir);
107 chdir $dir && &$wanted;
116 ($wanted, $dir, $nlink) = @_;
118 my($dev, $ino, $mode, $subcount);
120 # Get the list of files in the current directory.
121 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
122 my(@filenames) = readdir(DIR);
125 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
134 else { # This dir has subdirectories.
135 $subcount = $nlink - 2;
142 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
144 # Get link count and check for directoriness.
146 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
147 # unless ($nlink || $dont_use_nlink);
151 # It really is a directory, so do it recursively.
153 if (!$prune && chdir $_) {
154 $name =~ s/\.dir$// if $Is_VMS;
155 $name =~ s/\\dir$// if $Is_NT;
156 &finddir($wanted,$name,$nlink);
170 $cwd = Cwd::fastcwd();;
172 # Localize these rather than lexicalizing them for backwards
174 local($topdir, $topdev, $topino, $topmode, $topnlink);
175 foreach $topdir (@_) {
176 (($topdev,$topino,$topmode,$topnlink) =
177 ($Is_VMS ? stat($topdir) : lstat($topdir)))
178 || (warn("Can't stat $topdir: $!\n"), next);
180 if (chdir($topdir)) {
181 my $fixtopdir = $topdir;
182 $fixtopdir =~ s,/$,, ;
183 $fixtopdir =~ s/\.dir$// if $Is_VMS;
184 $fixtopdir =~ s/\\dir$// if $Is_NT;
185 &finddepthdir($wanted,$fixtopdir,$topnlink);
186 ($dir,$_) = ($fixtopdir,'.');
191 warn "Can't cd to $topdir: $!\n";
195 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
196 ($dir,$_) = ('.', $topdir);
199 chdir $dir && &$wanted;
208 ($wanted,$dir,$nlink) = @_;
209 my($dev, $ino, $mode, $subcount);
211 # Get the list of files in the current directory.
212 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
213 my(@filenames) = readdir(DIR);
216 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
225 else { # This dir has subdirectories.
226 $subcount = $nlink - 2;
232 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
234 # Get link count and check for directoriness.
236 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
240 # It really is a directory, so do it recursively.
243 $name =~ s/\.dir$// if $Is_VMS;
244 $name =~ s/\\dir$// if $Is_NT;
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 'msdos' || $^O eq 'amigaos';