1 .\" Copyright (c) 1993 Michael Haardt (michael@moria.de)
2 .\" and copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
3 .\" and copyright (c) 2006 Justin Pryzby <justinpryzby@users.sf.net>
4 .\" and copyright (c) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
7 .\" This is free documentation; you can redistribute it and/or
8 .\" modify it under the terms of the GNU General Public License as
9 .\" published by the Free Software Foundation; either version 2 of
10 .\" the License, or (at your option) any later version.
12 .\" The GNU General Public License's references to "object code"
13 .\" and "executables" are to be interpreted as the output of any
14 .\" document formatting or typesetting system, including
15 .\" intermediate and printed output.
17 .\" This manual is distributed in the hope that it will be useful,
18 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
19 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 .\" GNU General Public License for more details.
22 .\" You should have received a copy of the GNU General Public
23 .\" License along with this manual; if not, see
24 .\" <http://www.gnu.org/licenses/>.
27 .\" Modified Sun Jul 25 11:02:22 1993 by Rik Faith (faith@cs.unc.edu)
28 .\" 2006-05-24, Justin Pryzby <justinpryzby@users.sf.net>
29 .\" document FTW_ACTIONRETVAL; include .SH RETURN VALUE;
30 .\" 2006-05-24, Justin Pryzby <justinpryzby@users.sf.net> and
31 .\" Michael Kerrisk <mtk.manpages@gmail.com>
32 .\" reorganized and rewrote much of the page
33 .\" 2006-05-24, Michael Kerrisk <mtk.manpages@gmail.com>
34 .\" Added an example program.
36 .TH FTW 3 2019-03-06 "Linux" "Linux Programmer's Manual"
38 ftw, nftw \- file tree walk
43 .BI "int nftw(const char *" dirpath ,
44 .BI " int (*" fn ") (const char *" fpath ", const struct stat *" sb ,
45 .BI " int " typeflag ", struct FTW *" ftwbuf ),
46 .BI " int " nopenfd ", int " flags );
50 .BI "int ftw(const char *" dirpath ,
51 .BI " int (*" fn ") (const char *" fpath ", const struct stat *" sb ,
52 .BI " int " typeflag ),
53 .BI " int " nopenfd );
57 Feature Test Macro Requirements for glibc (see
58 .BR feature_test_macros (7)):
65 walks through the directory tree that is
66 located under the directory \fIdirpath\fP,
67 and calls \fIfn\fP() once for each entry in the tree.
68 By default, directories are handled before the files and
69 subdirectories they contain (preorder traversal).
71 To avoid using up all of the calling process's file descriptors,
72 \fInopenfd\fP specifies the maximum number of directories that
74 will hold open simultaneously.
76 the search depth exceeds this,
78 will become slower because
79 directories have to be closed and reopened.
82 one file descriptor for each level in the directory tree.
84 For each entry found in the tree,
87 \fIfn\fP() with four arguments:
94 is the pathname of the entry,
95 and is expressed either as a pathname relative to the calling process's
96 current working directory at the time of the call to
100 was expressed as a relative pathname,
101 or as an absolute pathname, if
103 was expressed as an absolute pathname.
107 structure returned by a call to
116 is an integer that has one of the following values:
128 is a directory which can't be read.
132 is a directory, and \fBFTW_DEPTH\fP was specified in \fIflags\fP.
137 then directories will always be visited with
142 and subdirectories within \fIfpath\fP have been processed.
149 which is not a symbolic link.
150 The probable cause for this is that the caller had read permission
151 on the parent directory, so that the filename
154 but did not have execute permission,
155 so that the file could not be reached for
157 The contents of the buffer pointed to by
163 is a symbolic link, and \fBFTW_PHYS\fP was set in \fIflags\fP.
164 .\" To obtain the definition of this constant from
168 .\" must be defined, or
169 .\" .BR _XOPEN_SOURCE
170 .\" must be defined with a value of 500 or more.
174 is a symbolic link pointing to a nonexistent file.
175 (This occurs only if \fBFTW_PHYS\fP is not set.)
176 On most implementations, in this case the
180 contains information returned by performing
182 on the symbolic link.
183 For the details on Linux, see BUGS.
189 supplies when calling
191 is a pointer to a structure of type \fIFTW\fP:
203 is the offset of the filename (i.e., basename component)
204 in the pathname given in
209 in the directory tree, relative to the root of the tree
213 To stop the tree walk, \fIfn\fP() returns a nonzero value; this
214 value will become the return value of
216 As long as \fIfn\fP() returns 0,
218 will continue either until it has traversed the entire tree,
219 in which case it will return zero,
220 or until it encounters an error (such as a
222 failure), in which case it will return \-1.
226 uses dynamic data structures, the only safe way to
227 exit out of a tree walk is to return a nonzero value from \fIfn\fP().
228 To allow a signal to terminate the walk without causing a memory leak,
229 have the handler set a global flag that is checked by \fIfn\fP().
232 unless the program is going to terminate.
234 The \fIflags\fP argument of
236 is formed by ORing zero or more of the
239 .BR FTW_ACTIONRETVAL " (since glibc 2.3.3)"
240 If this glibc-specific flag is set, then
242 handles the return value from
246 should return one of the following values:
252 to continue normally.
255 If \fIfn\fP() returns this value, then
256 siblings of the current entry will be skipped,
257 and processing continues in the parent.
258 .\" If \fBFTW_DEPTH\fP
259 .\" is set, the entry's parent directory is processed next (with
260 .\" \fIflag\fP set to \fBFTW_DP\fP).
263 If \fIfn\fP() is called with an entry that is a directory
264 (\fItypeflag\fP is \fBFTW_D\fP), this return
265 value will prevent objects within that directory from being passed as
266 arguments to \fIfn\fP().
268 continues processing with the next sibling of the directory.
273 to return immediately with the return value
276 Other return values could be associated with new actions in the future;
277 \fIfn\fP() should not return values other than those listed above.
279 The feature test macro
286 obtain the definition of \fBFTW_ACTIONRETVAL\fP from \fI<ftw.h>\fP.
292 to each directory before handling its contents.
293 This is useful if the program needs to perform some action
294 in the directory in which \fIfpath\fP resides.
295 (Specifying this flag has no effect on the pathname that is passed in the
301 If set, do a post-order traversal, that is, call \fIfn\fP() for
302 the directory itself \fIafter\fP handling the contents of the directory
303 and its subdirectories.
304 (By default, each directory is handled \fIbefore\fP its contents.)
307 If set, stay within the same filesystem
308 (i.e., do not cross mount points).
311 If set, do not follow symbolic links.
312 (This is what you want.)
313 If not set, symbolic links are followed, but no file is reported twice.
315 If \fBFTW_PHYS\fP is not set, but \fBFTW_DEPTH\fP is set,
318 is never called for a directory that would be a descendant of itself.
321 is an older function that offers a subset of the functionality of
323 The notable differences are as follows:
329 It behaves the same as when
335 The callback function,
337 is not supplied with a fourth argument.
339 The range of values that is passed via the
351 These functions return 0 on success, and \-1 if an error occurs.
353 If \fIfn\fP() returns nonzero,
354 then the tree walk is terminated and the value returned by \fIfn\fP()
355 is returned as the result of
362 is called with the \fBFTW_ACTIONRETVAL\fP flag,
363 then the only nonzero value that should be used by \fIfn\fP()
364 to terminate the tree walk is \fBFTW_STOP\fP,
365 and that value is returned as the result of
369 is available under glibc since version 2.1.
371 For an explanation of the terms used in this section, see
377 Interface Attribute Value
380 T} Thread safety MT-Safe cwd
383 T} Thread safety MT-Safe
387 POSIX.1-2001, POSIX.1-2008, SVr4, SUSv1.
392 POSIX.1-2008 notes that the results are unspecified if
394 does not preserve the current working directory.
398 and the use of \fBFTW_SL\fP with
400 were introduced in SUSv1.
402 In some implementations (e.g., glibc),
404 will never use \fBFTW_SL\fP, on other systems \fBFTW_SL\fP occurs only
405 for symbolic links that do not point to an existing file,
406 and again on other systems
408 will use \fBFTW_SL\fP for each symbolic link.
411 is a symbolic link and
413 failed, POSIX.1-2008 states
414 that it is undefined whether \fBFTW_NS\fP or \fBFTW_SL\fP
417 For predictable results, use
420 In the specification of
422 POSIX.1 notes that when
428 then the contents of the buffer pointed to by the
430 argument are undefined.
431 The standard makes no such statement for the case where
435 with the implication that the contents of the buffer pointed to by
438 And indeed this is the case on most implementations: the buffer pointed to by
440 contains the results produced by applying
442 to the symbolic link.
443 In early glibc, the behavior was the same.
444 However, since glibc 2.4, the contents of the buffer pointed to by
452 to be an unintended regression,
453 but it is not (yet) clear if the behavior will be restored to that
454 provided in the original glibc implementation (and on other implementations).
456 .\" https://bugzilla.redhat.com/show_bug.cgi?id=1422736
457 .\" http://austingroupbugs.net/view.php?id=1121
459 The following program traverses the directory tree under the path named
460 in its first command-line argument, or under the current directory
461 if no argument is supplied.
462 It displays various information about each file.
463 The second command-line argument can be used to specify characters that
464 control the value assigned to the \fIflags\fP
465 argument when calling
470 #define _XOPEN_SOURCE 500
478 display_info(const char *fpath, const struct stat *sb,
479 int tflag, struct FTW *ftwbuf)
482 (tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" :
483 (tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ? "f" :
484 (tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" :
485 (tflag == FTW_SLN) ? "sln" : "???",
489 printf("\-\-\-\-\-\-\-");
491 printf("%7jd", (intmax_t) sb\->st_size);
493 printf(" %\-40s %d %s\en",
494 fpath, ftwbuf\->base, fpath + ftwbuf\->base);
496 return 0; /* To tell nftw() to continue */
500 main(int argc, char *argv[])
504 if (argc > 2 && strchr(argv[2], \(aqd\(aq) != NULL)
506 if (argc > 2 && strchr(argv[2], \(aqp\(aq) != NULL)
509 if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags)