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 my ($topdir,$topdev,$topino,$topmode,$topnlink);
78 foreach $topdir (@_) {
79 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
80 || (warn("Can't stat $topdir: $!\n"), next);
83 ($dir,$_) = ($topdir,'.');
86 my $fixtopdir = $topdir;
87 $fixtopdir =~ s,/$,, ;
88 $fixtopdir =~ s/\.dir$// if $Is_VMS;
89 $fixtopdir =~ s/\\dir$// if $Is_NT;
90 &finddir($wanted,$fixtopdir,$topnlink);
93 warn "Can't cd to $topdir: $!\n";
97 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
98 ($dir,$_) = ('.', $topdir);
101 chdir $dir && &$wanted;
110 ($wanted, $dir, $nlink) = @_;
112 my($dev, $ino, $mode, $subcount);
114 # Get the list of files in the current directory.
115 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
116 my(@filenames) = readdir(DIR);
119 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
128 else { # This dir has subdirectories.
129 $subcount = $nlink - 2;
136 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
138 # Get link count and check for directoriness.
140 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
141 # unless ($nlink || $dont_use_nlink);
145 # It really is a directory, so do it recursively.
147 if (!$prune && chdir $_) {
148 $name =~ s/\.dir$// if $Is_VMS;
149 $name =~ s/\\dir$// if $Is_NT;
150 &finddir($wanted,$name,$nlink);
164 $cwd = Cwd::fastcwd();;
166 my($topdir, $topdev, $topino, $topmode, $topnlink);
167 foreach $topdir (@_) {
168 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
169 || (warn("Can't stat $topdir: $!\n"), next);
171 if (chdir($topdir)) {
172 my $fixtopdir = $topdir;
173 $fixtopdir =~ s,/$,, ;
174 $fixtopdir =~ s/\.dir$// if $Is_VMS;
175 $fixtopdir =~ s/\\dir$// if $Is_NT;
176 &finddepthdir($wanted,$fixtopdir,$topnlink);
177 ($dir,$_) = ($fixtopdir,'.');
182 warn "Can't cd to $topdir: $!\n";
186 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
187 ($dir,$_) = ('.', $topdir);
189 chdir $dir && &$wanted;
198 ($wanted,$dir,$nlink) = @_;
199 my($dev, $ino, $mode, $subcount);
201 # Get the list of files in the current directory.
202 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
203 my(@filenames) = readdir(DIR);
206 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
215 else { # This dir has subdirectories.
216 $subcount = $nlink - 2;
222 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
224 # Get link count and check for directoriness.
226 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
230 # It really is a directory, so do it recursively.
233 $name =~ s/\.dir$// if $Is_VMS;
234 $name =~ s/\\dir$// if $Is_NT;
235 &finddepthdir($wanted,$name,$nlink);
246 # Set dont_use_nlink in your hint file if your system's stat doesn't
247 # report the number of links in a directory as an indication
248 # of the number of files.
249 # See, e.g. hints/machten.sh for MachTen 2.2.
250 $dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
252 # These are hard-coded for now, but may move to hint files.
257 if ($^O =~ m:^mswin32:i) {
262 $dont_use_nlink = 1 if $^O eq 'os2';