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 This library is primarily for the C<find2perl> tool, which when fed,
36 find2perl / -name .nfs\* -mtime +7 \
37 -exec rm -f {} \; -o -fstype nfs -prune
39 produces something like:
43 (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
47 ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
49 ($File::Find::prune = 1);
52 Set the variable $File::Find::dont_use_nlink if you're using AFS,
55 C<finddepth> is just like C<find>, except that it does a depth-first
58 Here's another interesting wanted function. It will find all symlinks
62 -l && !-e && print "bogus link: $File::Find::name\n";
68 @EXPORT = qw(find finddepth);
73 my $cwd = Cwd::fastcwd();
74 my ($topdir,$topdev,$topino,$topmode,$topnlink);
75 foreach $topdir (@_) {
76 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
77 || (warn("Can't stat $topdir: $!\n"), next);
80 ($dir,$_) = ($topdir,'.');
83 my $fixtopdir = $topdir;
84 $fixtopdir =~ s,/$,, ;
85 $fixtopdir =~ s/\.dir$// if $Is_VMS;
86 $fixtopdir =~ s/\\dir$// if $Is_NT;
87 &finddir($wanted,$fixtopdir,$topnlink);
90 warn "Can't cd to $topdir: $!\n";
94 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
95 ($dir,$_) = ('.', $topdir);
98 chdir $dir && &$wanted;
107 ($wanted, $dir, $nlink) = @_;
109 my($dev, $ino, $mode, $subcount);
111 # Get the list of files in the current directory.
112 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
113 my(@filenames) = readdir(DIR);
116 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
125 else { # This dir has subdirectories.
126 $subcount = $nlink - 2;
133 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
135 # Get link count and check for directoriness.
137 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
138 # unless ($nlink || $dont_use_nlink);
142 # It really is a directory, so do it recursively.
144 if (!$prune && chdir $_) {
145 $name =~ s/\.dir$// if $Is_VMS;
146 $name =~ s/\\dir$// if $Is_NT;
147 &finddir($wanted,$name,$nlink);
161 $cwd = Cwd::fastcwd();;
163 my($topdir, $topdev, $topino, $topmode, $topnlink);
164 foreach $topdir (@_) {
165 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
166 || (warn("Can't stat $topdir: $!\n"), next);
168 if (chdir($topdir)) {
169 my $fixtopdir = $topdir;
170 $fixtopdir =~ s,/$,, ;
171 $fixtopdir =~ s/\.dir$// if $Is_VMS;
172 $fixtopdir =~ s/\\dir$// if $Is_NT;
173 &finddepthdir($wanted,$fixtopdir,$topnlink);
174 ($dir,$_) = ($fixtopdir,'.');
179 warn "Can't cd to $topdir: $!\n";
183 unless (($_,$dir) = File::Basename::fileparse($topdir)) {
184 ($dir,$_) = ('.', $topdir);
186 chdir $dir && &$wanted;
195 ($wanted,$dir,$nlink) = @_;
196 my($dev, $ino, $mode, $subcount);
198 # Get the list of files in the current directory.
199 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
200 my(@filenames) = readdir(DIR);
203 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
212 else { # This dir has subdirectories.
213 $subcount = $nlink - 2;
219 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
221 # Get link count and check for directoriness.
223 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
227 # It really is a directory, so do it recursively.
230 $name =~ s/\.dir$// if $Is_VMS;
231 $name =~ s/\\dir$// if $Is_NT;
232 &finddepthdir($wanted,$name,$nlink);
243 # Set dont_use_nlink in your hint file if your system's stat doesn't
244 # report the number of links in a directory as an indication
245 # of the number of files.
246 # See, e.g. hints/machten.sh for MachTen 2.2.
247 $dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
249 # These are hard-coded for now, but may move to hint files.
254 if ($^O =~ m:^mswin32:i) {
259 $dont_use_nlink = 1 if $^O eq 'os2';