10 find - traverse a file tree
12 finddepth - traverse a directory structure depth-first
17 find(\&wanted, '/foo','/bar');
21 finddepth(\&wanted, '/foo','/bar');
26 The wanted() function does whatever verifications you want. $dir contains
27 the current directory name, and $_ the current filename within that
28 directory. $name contains C<"$dir/$_">. You are chdir()'d to $dir when
29 the function is called. The function may set $prune to prune the tree.
31 This library is primarily for the C<find2perl> tool, which when fed,
33 find2perl / -name .nfs\* -mtime +7 \
34 -exec rm -f {} \; -o -fstype nfs -prune
36 produces something like:
40 (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
44 ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
49 Set the variable $dont_use_nlink if you're using AFS, since AFS cheats.
51 C<finddepth> is just like C<find>, except that it does a depth-first
54 Here's another interesting wanted function. It will find all symlinks
58 -l && !-e && print "bogus link: $name\n";
64 @EXPORT = qw(find finddepth $name $dir);
69 # find(\&wanted, '/foo','/bar');
72 # where wanted does whatever you want. $dir contains the
73 # current directory name, and $_ the current filename within
74 # that directory. $name contains "$dir/$_". You are cd'ed
75 # to $dir when the function is called. The function may
76 # set $prune to prune the tree.
78 # This library is primarily for find2perl, which, when fed
80 # find2perl / -name .nfs\* -mtime +7 -exec rm -f {} \; -o -fstype nfs -prune
82 # spits out something like this
86 # (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
90 # ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
95 # Set the variable $dont_use_nlink if you're using AFS, since AFS cheats.
100 foreach $topdir (@_) {
101 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
102 || (warn("Can't stat $topdir: $!\n"), next);
104 if (chdir($topdir)) {
105 ($dir,$_) = ($topdir,'.');
108 ($fixtopdir = $topdir) =~ s,/$,, ;
109 $fixtopdir =~ s/\.dir$// if $Is_VMS; ;
110 &finddir($wanted,$fixtopdir,$topnlink);
113 warn "Can't cd to $topdir: $!\n";
117 unless (($dir,$_) = fileparse($topdir)) {
118 ($dir,$_) = ('.', $topdir);
121 chdir $dir && &$wanted;
128 local($wanted,$dir,$nlink) = @_;
129 local($dev,$ino,$mode,$subcount);
132 # Get the list of files in the current directory.
134 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
135 local(@filenames) = readdir(DIR);
138 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
147 else { # This dir has subdirectories.
148 $subcount = $nlink - 2;
155 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
157 # Get link count and check for directoriness.
159 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_))
160 unless ($nlink || $dont_use_nlink);
164 # It really is a directory, so do it recursively.
166 if (!$prune && chdir $_) {
167 $name =~ s/\.dir$// if $Is_VMS;
168 &finddir($wanted,$name,$nlink);
181 # finddepth(\&wanted, '/foo','/bar');
184 # where wanted does whatever you want. $dir contains the
185 # current directory name, and $_ the current filename within
186 # that directory. $name contains "$dir/$_". You are cd'ed
187 # to $dir when the function is called. The function may
188 # set $prune to prune the tree.
190 # This library is primarily for find2perl, which, when fed
192 # find2perl / -name .nfs\* -mtime +7 -exec rm -f {} \; -o -fstype nfs -prune
194 # spits out something like this
198 # (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
202 # ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
210 foreach $topdir (@_) {
211 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
212 || (warn("Can't stat $topdir: $!\n"), next);
214 if (chdir($topdir)) {
215 ($fixtopdir = $topdir) =~ s,/$,, ;
216 $fixtopdir =~ s/\.dir$// if $Is_VMS;
217 &finddepthdir($wanted,$fixtopdir,$topnlink);
218 ($dir,$_) = ($fixtopdir,'.');
223 warn "Can't cd to $topdir: $!\n";
227 unless (($dir,$_) = fileparse($topdir)) {
228 ($dir,$_) = ('.', $topdir);
230 chdir $dir && &$wanted;
237 my($wanted,$dir,$nlink) = @_;
238 my($dev,$ino,$mode,$subcount);
241 # Get the list of files in the current directory.
243 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
244 my(@filenames) = readdir(DIR);
247 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
256 else { # This dir has subdirectories.
257 $subcount = $nlink - 2;
263 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
265 # Get link count and check for directoriness.
267 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
271 # It really is a directory, so do it recursively.
273 if (!$prune && chdir $_) {
274 $name =~ s/\.dir$// if $Is_VMS;
275 &finddepthdir($wanted,$name,$nlink);
286 if ($Config{'osname'} eq 'VMS') {