summaryrefslogtreecommitdiffstats
path: root/upstream/debian-unstable/man3/File::Find.3perl
blob: a1425f20ed3b6aa922a0af2c11b949c897da8296 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
.    if \nF \{\
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{\
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "File::Find 3perl"
.TH File::Find 3perl 2024-05-30 "perl v5.38.2" "Perl Programmers Reference Guide"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH NAME
File::Find \- Traverse a directory tree.
.SH SYNOPSIS
.IX Header "SYNOPSIS"
.Vb 3
\&    use File::Find;
\&    find(\e&wanted, @directories_to_search);
\&    sub wanted { ... }
\&
\&    use File::Find;
\&    finddepth(\e&wanted, @directories_to_search);
\&    sub wanted { ... }
\&
\&    use File::Find;
\&    find({ wanted => \e&process, follow => 1 }, \*(Aq.\*(Aq);
.Ve
.SH DESCRIPTION
.IX Header "DESCRIPTION"
These are functions for searching through directory trees doing work
on each file found similar to the Unix \fIfind\fR command.  File::Find
exports two functions, \f(CW\*(C`find\*(C'\fR and \f(CW\*(C`finddepth\*(C'\fR.  They work similarly
but have subtle differences.
.IP \fBfind\fR 4
.IX Item "find"
.Vb 2
\&  find(\e&wanted,  @directories);
\&  find(\e%options, @directories);
.Ve
.Sp
\&\f(CWfind()\fR does a depth-first search over the given \f(CW@directories\fR in
the order they are given.  For each file or directory found, it calls
the \f(CW&wanted\fR subroutine.  (See below for details on how to use the
\&\f(CW&wanted\fR function).  Additionally, for each directory found, it will
\&\f(CWchdir()\fR into that directory and continue the search, invoking the
\&\f(CW&wanted\fR function on each file or subdirectory in the directory.
.IP \fBfinddepth\fR 4
.IX Item "finddepth"
.Vb 2
\&  finddepth(\e&wanted,  @directories);
\&  finddepth(\e%options, @directories);
.Ve
.Sp
\&\f(CWfinddepth()\fR works just like \f(CWfind()\fR except that it invokes the
\&\f(CW&wanted\fR function for a directory \fIafter\fR invoking it for the
directory's contents.  It does a postorder traversal instead of a
preorder traversal, working from the bottom of the directory tree up
where \f(CWfind()\fR works from the top of the tree down.
.PP
Despite the name of the \f(CWfinddepth()\fR function, both \f(CWfind()\fR and
\&\f(CWfinddepth()\fR perform a depth-first search of the directory hierarchy.
.ie n .SS %options
.el .SS \f(CW%options\fP
.IX Subsection "%options"
The first argument to \f(CWfind()\fR is either a code reference to your
\&\f(CW&wanted\fR function, or a hash reference describing the operations
to be performed for each file.  The
code reference is described in "The wanted function" below.
.PP
Here are the possible keys for the hash:
.ie n .IP """wanted""" 4
.el .IP \f(CWwanted\fR 4
.IX Item "wanted"
The value should be a code reference.  This code reference is
described in "The wanted function" below. The \f(CW&wanted\fR subroutine is
mandatory.
.ie n .IP """bydepth""" 4
.el .IP \f(CWbydepth\fR 4
.IX Item "bydepth"
Reports the name of a directory only AFTER all its entries
have been reported.  Entry point \f(CWfinddepth()\fR is a shortcut for
specifying \f(CW\*(C`{ bydepth => 1 }\*(C'\fR in the first argument of \f(CWfind()\fR.
.ie n .IP """preprocess""" 4
.el .IP \f(CWpreprocess\fR 4
.IX Item "preprocess"
The value should be a code reference. This code reference is used to
preprocess the current directory. The name of the currently processed
directory is in \f(CW$File::Find::dir\fR. Your preprocessing function is
called after \f(CWreaddir()\fR, but before the loop that calls the \f(CWwanted()\fR
function. It is called with a list of strings (actually file/directory
names) and is expected to return a list of strings. The code can be
used to sort the file/directory names alphabetically, numerically,
or to filter out directory entries based on their name alone. When
\&\fIfollow\fR or \fIfollow_fast\fR are in effect, \f(CW\*(C`preprocess\*(C'\fR is a no-op.
.ie n .IP """postprocess""" 4
.el .IP \f(CWpostprocess\fR 4
.IX Item "postprocess"
The value should be a code reference. It is invoked just before leaving
the currently processed directory. It is called in void context with no
arguments. The name of the current directory is in \f(CW$File::Find::dir\fR. This
hook is handy for summarizing a directory, such as calculating its disk
usage. When \fIfollow\fR or \fIfollow_fast\fR are in effect, \f(CW\*(C`postprocess\*(C'\fR is a
no-op.
.ie n .IP """follow""" 4
.el .IP \f(CWfollow\fR 4
.IX Item "follow"
Causes symbolic links to be followed. Since directory trees with symbolic
links (followed) may contain files more than once and may even have
cycles, a hash has to be built up with an entry for each file.
This might be expensive both in space and time for a large
directory tree. See "follow_fast" and "follow_skip" below.
If either \fIfollow\fR or \fIfollow_fast\fR is in effect:
.RS 4
.IP \(bu 4
It is guaranteed that an \fIlstat\fR has been called before the user's
\&\f(CWwanted()\fR function is called. This enables fast file checks involving \f(CW\*(C`_\*(C'\fR.
Note that this guarantee no longer holds if \fIfollow\fR or \fIfollow_fast\fR
are not set.
.IP \(bu 4
There is a variable \f(CW$File::Find::fullname\fR which holds the absolute
pathname of the file with all symbolic links resolved.  If the link is
a dangling symbolic link, then fullname will be set to \f(CW\*(C`undef\*(C'\fR.
.RE
.RS 4
.RE
.ie n .IP """follow_fast""" 4
.el .IP \f(CWfollow_fast\fR 4
.IX Item "follow_fast"
This is similar to \fIfollow\fR except that it may report some files more
than once.  It does detect cycles, however.  Since only symbolic links
have to be hashed, this is much cheaper both in space and time.  If
processing a file more than once (by the user's \f(CWwanted()\fR function)
is worse than just taking time, the option \fIfollow\fR should be used.
.ie n .IP """follow_skip""" 4
.el .IP \f(CWfollow_skip\fR 4
.IX Item "follow_skip"
\&\f(CW\*(C`follow_skip==1\*(C'\fR, which is the default, causes all files which are
neither directories nor symbolic links to be ignored if they are about
to be processed a second time. If a directory or a symbolic link
are about to be processed a second time, File::Find dies.
.Sp
\&\f(CW\*(C`follow_skip==0\*(C'\fR causes File::Find to die if any file is about to be
processed a second time.
.Sp
\&\f(CW\*(C`follow_skip==2\*(C'\fR causes File::Find to ignore any duplicate files and
directories but to proceed normally otherwise.
.ie n .IP """dangling_symlinks""" 4
.el .IP \f(CWdangling_symlinks\fR 4
.IX Item "dangling_symlinks"
Specifies what to do with symbolic links whose target doesn't exist.
If true and a code reference, will be called with the symbolic link
name and the directory it lives in as arguments.  Otherwise, if true
and warnings are on, a warning of the form \f(CW"symbolic_link_name is a dangling
symbolic link\en"\fR will be issued.  If false, the dangling symbolic link
will be silently ignored.
.ie n .IP """no_chdir""" 4
.el .IP \f(CWno_chdir\fR 4
.IX Item "no_chdir"
Does not \f(CWchdir()\fR to each directory as it recurses. The \f(CWwanted()\fR
function will need to be aware of this, of course. In this case,
\&\f(CW$_\fR will be the same as \f(CW$File::Find::name\fR.
.ie n .IP """untaint""" 4
.el .IP \f(CWuntaint\fR 4
.IX Item "untaint"
If find is used in taint-mode (\-T command line switch or
if EUID != UID or if EGID != GID), then internally directory names have to be
untainted before they can be \f(CW\*(C`chdir\*(C'\fR'd to. Therefore they are checked against
a regular expression \fIuntaint_pattern\fR.  Note that all names passed to the
user's \f(CWwanted()\fR function are still tainted. If this option is used while not
in taint-mode, \f(CW\*(C`untaint\*(C'\fR is a no-op.
.ie n .IP """untaint_pattern""" 4
.el .IP \f(CWuntaint_pattern\fR 4
.IX Item "untaint_pattern"
See above. This should be set using the \f(CW\*(C`qr\*(C'\fR quoting operator.
The default is set to \f(CW\*(C`qr|^([\-+@\ew./]+)$|\*(C'\fR.
Note that the parentheses are vital.
.ie n .IP """untaint_skip""" 4
.el .IP \f(CWuntaint_skip\fR 4
.IX Item "untaint_skip"
If set, a directory which fails the \fIuntaint_pattern\fR is skipped,
including all its sub-directories. The default is to \f(CW\*(C`die\*(C'\fR in such a case.
.SS "The wanted function"
.IX Subsection "The wanted function"
The \f(CWwanted()\fR function does whatever verifications you want on
each file and directory.  Note that despite its name, the \f(CWwanted()\fR
function is a generic callback function, and does \fBnot\fR tell
File::Find if a file is "wanted" or not.  In fact, its return value
is ignored.
.PP
The wanted function takes no arguments but rather does its work
through a collection of variables.
.ie n .IP "$File::Find::dir is the current directory name," 4
.el .IP "\f(CW$File::Find::dir\fR is the current directory name," 4
.IX Item "$File::Find::dir is the current directory name,"
.PD 0
.ie n .IP "$_ is the current filename within that directory" 4
.el .IP "\f(CW$_\fR is the current filename within that directory" 4
.IX Item "$_ is the current filename within that directory"
.ie n .IP "$File::Find::name is the complete pathname to the file." 4
.el .IP "\f(CW$File::Find::name\fR is the complete pathname to the file." 4
.IX Item "$File::Find::name is the complete pathname to the file."
.PD
.PP
The above variables have all been localized and may be changed without
affecting data outside of the wanted function.
.PP
For example, when examining the file \fI/some/path/foo.ext\fR you will have:
.PP
.Vb 3
\&    $File::Find::dir  = /some/path/
\&    $_                = foo.ext
\&    $File::Find::name = /some/path/foo.ext
.Ve
.PP
You are \fBchdir()\fR'd to \f(CW$File::Find::dir\fR when the function is called,
unless \f(CW\*(C`no_chdir\*(C'\fR was specified. Note that when changing to
directories is in effect, the root directory (\fI/\fR) is a somewhat
special case inasmuch as the concatenation of \f(CW$File::Find::dir\fR,
\&\f(CW\*(Aq/\*(Aq\fR and \f(CW$_\fR is not literally equal to \f(CW$File::Find::name\fR. The
table below summarizes all variants:
.PP
.Vb 4
\&              $File::Find::name  $File::Find::dir  $_
\& default      /                  /                 .
\& no_chdir=>0  /etc               /                 etc
\&              /etc/x             /etc              x
\&
\& no_chdir=>1  /                  /                 /
\&              /etc               /                 /etc
\&              /etc/x             /etc              /etc/x
.Ve
.PP
When \f(CW\*(C`follow\*(C'\fR or \f(CW\*(C`follow_fast\*(C'\fR are in effect, there is
also a \f(CW$File::Find::fullname\fR.  The function may set
\&\f(CW$File::Find::prune\fR to prune the tree unless \f(CW\*(C`bydepth\*(C'\fR was
specified.  Unless \f(CW\*(C`follow\*(C'\fR or \f(CW\*(C`follow_fast\*(C'\fR is specified, for
compatibility reasons (find.pl, find2perl) there are in addition the
following globals available: \f(CW$File::Find::topdir\fR,
\&\f(CW$File::Find::topdev\fR, \f(CW$File::Find::topino\fR,
\&\f(CW$File::Find::topmode\fR and \f(CW$File::Find::topnlink\fR.
.PP
This library is useful for the \f(CW\*(C`find2perl\*(C'\fR tool (distributed as part of the
App\-find2perl CPAN distribution), which when fed,
.PP
.Vb 2
\&  find2perl / \-name .nfs\e* \-mtime +7 \e
\&    \-exec rm \-f {} \e; \-o \-fstype nfs \-prune
.Ve
.PP
produces something like:
.PP
.Vb 10
\& sub wanted {
\&    /^\e.nfs.*\ez/s &&
\&    (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_)) &&
\&    int(\-M _) > 7 &&
\&    unlink($_)
\&    ||
\&    ($nlink || (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_))) &&
\&    $dev < 0 &&
\&    ($File::Find::prune = 1);
\& }
.Ve
.PP
Notice the \f(CW\*(C`_\*(C'\fR in the above \f(CW\*(C`int(\-M _)\*(C'\fR: the \f(CW\*(C`_\*(C'\fR is a magical
filehandle that caches the information from the preceding
\&\f(CWstat()\fR, \f(CWlstat()\fR, or filetest.
.PP
Here's another interesting wanted function.  It will find all symbolic
links that don't resolve:
.PP
.Vb 3
\&    sub wanted {
\&         \-l && !\-e && print "bogus link: $File::Find::name\en";
\&    }
.Ve
.PP
Note that you may mix directories and (non-directory) files in the list of 
directories to be searched by the \f(CWwanted()\fR function.
.PP
.Vb 1
\&    find(\e&wanted, "./foo", "./bar", "./baz/epsilon");
.Ve
.PP
In the example above, no file in \fI./baz/\fR other than \fI./baz/epsilon\fR will be
evaluated by \f(CWwanted()\fR.
.PP
See also the script \f(CW\*(C`pfind\*(C'\fR on CPAN for a nice application of this
module.
.SH WARNINGS
.IX Header "WARNINGS"
If you run your program with the \f(CW\*(C`\-w\*(C'\fR switch, or if you use the
\&\f(CW\*(C`warnings\*(C'\fR pragma, File::Find will report warnings for several weird
situations. You can disable these warnings by putting the statement
.PP
.Vb 1
\&    no warnings \*(AqFile::Find\*(Aq;
.Ve
.PP
in the appropriate scope. See warnings for more info about lexical
warnings.
.SH "BUGS AND CAVEATS"
.IX Header "BUGS AND CAVEATS"
.ie n .IP $dont_use_nlink 4
.el .IP \f(CW$dont_use_nlink\fR 4
.IX Item "$dont_use_nlink"
You can set the variable \f(CW$File::Find::dont_use_nlink\fR to 0 if you
are sure the filesystem you are scanning reflects the number of
subdirectories in the parent directory's \f(CW\*(C`nlink\*(C'\fR count.
.Sp
If you do set \f(CW$File::Find::dont_use_nlink\fR to 0, you may notice an
improvement in speed at the risk of not recursing into subdirectories
if a filesystem doesn't populate \f(CW\*(C`nlink\*(C'\fR as expected.
.Sp
\&\f(CW$File::Find::dont_use_nlink\fR now defaults to 1 on all platforms.
.IP symlinks 4
.IX Item "symlinks"
Be aware that the option to follow symbolic links can be dangerous.
Depending on the structure of the directory tree (including symbolic
links to directories) you might traverse a given (physical) directory
more than once (only if \f(CW\*(C`follow_fast\*(C'\fR is in effect).
Furthermore, deleting or changing files in a symbolically linked directory
might cause very unpleasant surprises, since you delete or change files
in an unknown directory.
.SH HISTORY
.IX Header "HISTORY"
File::Find used to produce incorrect results if called recursively.
During the development of perl 5.8 this bug was fixed.
The first fixed version of File::Find was 1.01.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
\&\fBfind\fR\|(1), find2perl.