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 &finddir($wanted,$fixtopdir,$topnlink);
89 warn "Can't cd to $topdir: $!\n";
93 unless (($dir,$_) = File::Basename::fileparse($topdir)) {
94 ($dir,$_) = ('.', $topdir);
97 chdir $dir && &$wanted;
106 ($wanted, $dir, $nlink) = @_;
108 my($dev, $ino, $mode, $subcount);
110 # Get the list of files in the current directory.
111 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
112 my(@filenames) = readdir(DIR);
115 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
124 else { # This dir has subdirectories.
125 $subcount = $nlink - 2;
132 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
134 # Get link count and check for directoriness.
136 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_))
137 unless ($nlink || $dont_use_nlink);
141 # It really is a directory, so do it recursively.
143 if (!$prune && chdir $_) {
144 $name =~ s/\.dir$// if $Is_VMS;
145 &finddir($wanted,$name,$nlink);
159 $cwd = Cwd::fastcwd();;
161 my($topdir, $topdev, $topino, $topmode, $topnlink);
162 foreach $topdir (@_) {
163 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
164 || (warn("Can't stat $topdir: $!\n"), next);
166 if (chdir($topdir)) {
167 my $fixtopdir = $topdir;
168 $fixtopdir =~ s,/$,, ;
169 $fixtopdir =~ s/\.dir$// if $Is_VMS;
170 &finddepthdir($wanted,$fixtopdir,$topnlink);
171 ($dir,$_) = ($fixtopdir,'.');
176 warn "Can't cd to $topdir: $!\n";
180 unless (($dir,$_) = File::Basename::fileparse($topdir)) {
181 ($dir,$_) = ('.', $topdir);
183 chdir $dir && &$wanted;
192 ($wanted,$dir,$nlink) = @_;
193 my($dev, $ino, $mode, $subcount);
195 # Get the list of files in the current directory.
196 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
197 my(@filenames) = readdir(DIR);
200 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
209 else { # This dir has subdirectories.
210 $subcount = $nlink - 2;
216 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
218 # Get link count and check for directoriness.
220 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
224 # It really is a directory, so do it recursively.
227 $name =~ s/\.dir$// if $Is_VMS;
228 &finddepthdir($wanted,$name,$nlink);
239 # Set dont_use_nlink in your hint file if your system's stat doesn't
240 # report the number of links in a directory as an indication
241 # of the number of files.
242 # See, e.g. hints/machten.sh for MachTen 2.2.
243 $dont_use_nlink = 1 if ($Config::Config{'dont_use_nlink'});
245 # These are hard-coded for now, but may move to hint files.
246 if ($Config::Config{'osname'} eq 'VMS') {
251 $dont_use_nlink = 1 if $Config::Config{'osname'} =~ m:^os/?2$:i ;
252 $dont_use_nlink = 1 if $Config::Config{'osname'} =~ m:^mswin32$:i ;