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 .\" SPDX-License-Identifier: GPL-2.0-or-later
8 .\" Modified Sun Jul 25 11:02:22 1993 by Rik Faith (faith@cs.unc.edu)
9 .\" 2006-05-24, Justin Pryzby <justinpryzby@users.sf.net>
10 .\" document FTW_ACTIONRETVAL; include .SH RETURN VALUE;
11 .\" 2006-05-24, Justin Pryzby <justinpryzby@users.sf.net> and
12 .\" Michael Kerrisk <mtk.manpages@gmail.com>
13 .\" reorganized and rewrote much of the page
14 .\" 2006-05-24, Michael Kerrisk <mtk.manpages@gmail.com>
15 .\" Added an example program.
17 .TH FTW 3 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
19 ftw, nftw \- file tree walk
22 .RI ( libc ", " \-lc )
27 .BI "int nftw(const char *" dirpath ,
28 .BI " int (*" fn ")(const char *" fpath ", const struct stat *" sb ,
29 .BI " int " typeflag ", struct FTW *" ftwbuf ),
30 .BI " int " nopenfd ", int " flags );
32 .BI "int ftw(const char *" dirpath ,
33 .BI " int (*" fn ")(const char *" fpath ", const struct stat *" sb ,
34 .BI " int " typeflag ),
35 .BI " int " nopenfd );
39 Feature Test Macro Requirements for glibc (see
40 .BR feature_test_macros (7)):
49 walks through the directory tree that is
50 located under the directory \fIdirpath\fP,
51 and calls \fIfn\fP() once for each entry in the tree.
52 By default, directories are handled before the files and
53 subdirectories they contain (preorder traversal).
55 To avoid using up all of the calling process's file descriptors,
56 \fInopenfd\fP specifies the maximum number of directories that
58 will hold open simultaneously.
60 the search depth exceeds this,
62 will become slower because
63 directories have to be closed and reopened.
66 one file descriptor for each level in the directory tree.
68 For each entry found in the tree,
71 \fIfn\fP() with four arguments:
78 is the pathname of the entry,
79 and is expressed either as a pathname relative to the calling process's
80 current working directory at the time of the call to
84 was expressed as a relative pathname,
85 or as an absolute pathname, if
87 was expressed as an absolute pathname.
91 structure returned by a call to
100 is an integer that has one of the following values:
112 is a directory which can't be read.
116 is a directory, and \fBFTW_DEPTH\fP was specified in \fIflags\fP.
121 then directories will always be visited with
126 and subdirectories within \fIfpath\fP have been processed.
133 which is not a symbolic link.
134 The probable cause for this is that the caller had read permission
135 on the parent directory, so that the filename
138 but did not have execute permission,
139 so that the file could not be reached for
141 The contents of the buffer pointed to by
147 is a symbolic link, and \fBFTW_PHYS\fP was set in \fIflags\fP.
148 .\" To obtain the definition of this constant from
152 .\" must be defined, or
153 .\" .BR _XOPEN_SOURCE
154 .\" must be defined with a value of 500 or more.
158 is a symbolic link pointing to a nonexistent file.
159 (This occurs only if \fBFTW_PHYS\fP is not set.)
164 contains information returned by performing
166 on the "dangling" symbolic link.
173 supplies when calling
175 is a pointer to a structure of type \fIFTW\fP:
187 is the offset of the filename (i.e., basename component)
188 in the pathname given in
193 in the directory tree, relative to the root of the tree
197 To stop the tree walk, \fIfn\fP() returns a nonzero value; this
198 value will become the return value of
200 As long as \fIfn\fP() returns 0,
202 will continue either until it has traversed the entire tree,
203 in which case it will return zero,
204 or until it encounters an error (such as a
206 failure), in which case it will return \-1.
210 uses dynamic data structures, the only safe way to
211 exit out of a tree walk is to return a nonzero value from \fIfn\fP().
212 To allow a signal to terminate the walk without causing a memory leak,
213 have the handler set a global flag that is checked by \fIfn\fP().
216 unless the program is going to terminate.
218 The \fIflags\fP argument of
220 is formed by ORing zero or more of the
223 .BR FTW_ACTIONRETVAL " (since glibc 2.3.3)"
224 If this glibc-specific flag is set, then
226 handles the return value from
230 should return one of the following values:
236 to continue normally.
239 If \fIfn\fP() returns this value, then
240 siblings of the current entry will be skipped,
241 and processing continues in the parent.
242 .\" If \fBFTW_DEPTH\fP
243 .\" is set, the entry's parent directory is processed next (with
244 .\" \fIflag\fP set to \fBFTW_DP\fP).
247 If \fIfn\fP() is called with an entry that is a directory
248 (\fItypeflag\fP is \fBFTW_D\fP), this return
249 value will prevent objects within that directory from being passed as
250 arguments to \fIfn\fP().
252 continues processing with the next sibling of the directory.
257 to return immediately with the return value
260 Other return values could be associated with new actions in the future;
261 \fIfn\fP() should not return values other than those listed above.
263 The feature test macro
270 obtain the definition of \fBFTW_ACTIONRETVAL\fP from \fI<ftw.h>\fP.
276 to each directory before handling its contents.
277 This is useful if the program needs to perform some action
278 in the directory in which \fIfpath\fP resides.
279 (Specifying this flag has no effect on the pathname that is passed in the
285 If set, do a post-order traversal, that is, call \fIfn\fP() for
286 the directory itself \fIafter\fP handling the contents of the directory
287 and its subdirectories.
288 (By default, each directory is handled \fIbefore\fP its contents.)
291 If set, stay within the same filesystem
292 (i.e., do not cross mount points).
295 If set, do not follow symbolic links.
296 (This is what you want.)
297 If not set, symbolic links are followed, but no file is reported twice.
299 If \fBFTW_PHYS\fP is not set, but \fBFTW_DEPTH\fP is set,
302 is never called for a directory that would be a descendant of itself.
305 is an older function that offers a subset of the functionality of
307 The notable differences are as follows:
313 It behaves the same as when
319 The callback function,
321 is not supplied with a fourth argument.
323 The range of values that is passed via the
335 These functions return 0 on success, and \-1 if an error occurs.
337 If \fIfn\fP() returns nonzero,
338 then the tree walk is terminated and the value returned by \fIfn\fP()
339 is returned as the result of
346 is called with the \fBFTW_ACTIONRETVAL\fP flag,
347 then the only nonzero value that should be used by \fIfn\fP()
348 to terminate the tree walk is \fBFTW_STOP\fP,
349 and that value is returned as the result of
353 is available under glibc since version 2.1.
355 For an explanation of the terms used in this section, see
363 Interface Attribute Value
366 T} Thread safety MT-Safe cwd
369 T} Thread safety MT-Safe
375 POSIX.1-2001, POSIX.1-2008, SVr4, SUSv1.
380 POSIX.1-2008 notes that the results are unspecified if
382 does not preserve the current working directory.
386 and the use of \fBFTW_SL\fP with
388 were introduced in SUSv1.
390 In some implementations (e.g., glibc),
392 will never use \fBFTW_SL\fP, on other systems \fBFTW_SL\fP occurs only
393 for symbolic links that do not point to an existing file,
394 and again on other systems
396 will use \fBFTW_SL\fP for each symbolic link.
399 is a symbolic link and
401 failed, POSIX.1-2008 states
402 that it is undefined whether \fBFTW_NS\fP or \fBFTW_SL\fP
405 For predictable results, use
408 According to POSIX.1-2008, when the
414 the buffer pointed to by
416 should contain information about the dangling symbolic link
420 Early glibc versions correctly followed the POSIX specification on this point.
421 However, as a result of a regression introduced in glibc 2.4,
422 the contents of the buffer pointed to by
428 (More precisely, the contents of the buffer were left unchanged in this case.)
429 This regression was eventually fixed in glibc 2.30,
430 .\" https://bugzilla.redhat.com/show_bug.cgi?id=1422736
431 .\" http://austingroupbugs.net/view.php?id=1121
432 .\" glibc commit 6ba205b2c35e3e024c8c12d2ee1b73363e84da87
433 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=23501
434 so that the glibc implementation (once more) follows the POSIX specification.
436 The following program traverses the directory tree under the path named
437 in its first command-line argument, or under the current directory
438 if no argument is supplied.
439 It displays various information about each file.
440 The second command-line argument can be used to specify characters that
441 control the value assigned to the \fIflags\fP
442 argument when calling
447 #define _XOPEN_SOURCE 500
455 display_info(const char *fpath, const struct stat *sb,
456 int tflag, struct FTW *ftwbuf)
459 (tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" :
460 (tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ? "f" :
461 (tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" :
462 (tflag == FTW_SLN) ? "sln" : "???",
466 printf("\-\-\-\-\-\-\-");
468 printf("%7jd", (intmax_t) sb\->st_size);
470 printf(" %\-40s %d %s\en",
471 fpath, ftwbuf\->base, fpath + ftwbuf\->base);
473 return 0; /* To tell nftw() to continue */
477 main(int argc, char *argv[])
481 if (argc > 2 && strchr(argv[2], \(aqd\(aq) != NULL)
483 if (argc > 2 && strchr(argv[2], \(aqp\(aq) != NULL)
486 if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags)