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);
66 $dont_use_nlink = 1 if $Config{osname} =~ m:^os/?2$:i ;
71 # find(\&wanted, '/foo','/bar');
74 # where wanted does whatever you want. $dir contains the
75 # current directory name, and $_ the current filename within
76 # that directory. $name contains "$dir/$_". You are cd'ed
77 # to $dir when the function is called. The function may
78 # set $prune to prune the tree.
80 # This library is primarily for find2perl, which, when fed
82 # find2perl / -name .nfs\* -mtime +7 -exec rm -f {} \; -o -fstype nfs -prune
84 # spits out something like this
88 # (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
92 # ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
97 # Set the variable $dont_use_nlink if you're using AFS, since AFS cheats.
102 foreach $topdir (@_) {
103 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
104 || (warn("Can't stat $topdir: $!\n"), next);
106 if (chdir($topdir)) {
107 ($dir,$_) = ($topdir,'.');
110 ($fixtopdir = $topdir) =~ s,/$,, ;
111 $fixtopdir =~ s/\.dir$// if $Is_VMS; ;
112 &finddir($wanted,$fixtopdir,$topnlink);
115 warn "Can't cd to $topdir: $!\n";
119 unless (($dir,$_) = fileparse($topdir)) {
120 ($dir,$_) = ('.', $topdir);
123 chdir $dir && &$wanted;
130 local($wanted,$dir,$nlink) = @_;
131 local($dev,$ino,$mode,$subcount);
134 # Get the list of files in the current directory.
136 opendir(DIR,'.') || (warn "Can't open $dir: $!\n", return);
137 local(@filenames) = readdir(DIR);
140 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
149 else { # This dir has subdirectories.
150 $subcount = $nlink - 2;
157 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
159 # Get link count and check for directoriness.
161 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_))
162 unless ($nlink || $dont_use_nlink);
166 # It really is a directory, so do it recursively.
168 if (!$prune && chdir $_) {
169 $name =~ s/\.dir$// if $Is_VMS;
170 &finddir($wanted,$name,$nlink);
183 # finddepth(\&wanted, '/foo','/bar');
186 # where wanted does whatever you want. $dir contains the
187 # current directory name, and $_ the current filename within
188 # that directory. $name contains "$dir/$_". You are cd'ed
189 # to $dir when the function is called. The function may
190 # set $prune to prune the tree.
192 # This library is primarily for find2perl, which, when fed
194 # find2perl / -name .nfs\* -mtime +7 -exec rm -f {} \; -o -fstype nfs -prune
196 # spits out something like this
200 # (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
204 # ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
212 foreach $topdir (@_) {
213 (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
214 || (warn("Can't stat $topdir: $!\n"), next);
216 if (chdir($topdir)) {
217 ($fixtopdir = $topdir) =~ s,/$,, ;
218 $fixtopdir =~ s/\.dir$// if $Is_VMS;
219 &finddepthdir($wanted,$fixtopdir,$topnlink);
220 ($dir,$_) = ($fixtopdir,'.');
225 warn "Can't cd to $topdir: $!\n";
229 unless (($dir,$_) = fileparse($topdir)) {
230 ($dir,$_) = ('.', $topdir);
232 chdir $dir && &$wanted;
239 my($wanted,$dir,$nlink) = @_;
240 my($dev,$ino,$mode,$subcount);
241 local($name); # so &wanted sees current value
243 # Get the list of files in the current directory.
245 opendir(DIR,'.') || warn "Can't open $dir: $!\n";
246 my(@filenames) = readdir(DIR);
249 if ($nlink == 2 && !$dont_use_nlink) { # This dir has no subdirectories.
258 else { # This dir has subdirectories.
259 $subcount = $nlink - 2;
265 if ($subcount > 0 || $dont_use_nlink) { # Seen all the subdirs?
267 # Get link count and check for directoriness.
269 ($dev,$ino,$mode,$nlink) = ($Is_VMS ? stat($_) : lstat($_));
273 # It really is a directory, so do it recursively.
275 if (!$prune && chdir $_) {
276 $name =~ s/\.dir$// if $Is_VMS;
277 &finddepthdir($wanted,$name,$nlink);
288 if ($Config{'osname'} eq 'VMS') {