fts(3)
๐ NAME ๐
so this is fts(3) from the Library Functions Manual, bestie โจ and the whole squad is: fts, fts_open, fts_read, fts_children, fts_set, fts_close โ traverse a file hierarchy ๐
๐ LIBRARY ๐
She lives in the Standard C library (libc, link with -lc) ๐ซถ
โจ SYNOPSIS โจ
#include <sys/types.h>
#include <sys/stat.h>
#include <fts.h>
FTS *fts_open(char *const *path_argv, int options,
typeof(int (const FTSENT **, const FTSENT **))
*_Nullable compar);
FTSENT *fts_read(FTS *ftsp);
FTSENT *fts_children(FTS *ftsp, int instr);
int fts_set(FTS *ftsp, FTSENT *f, int instr);
int fts_close(FTS *ftsp);
๐ธ DESCRIPTION ๐ธ
okay so the fts functions are literally here for traversing file hierarchies. The quick vibe check: fts_open() returns a โhandleโ (of type FTS *) that refers to a file hierarchy โstreamโ ๐. Then you hand that handle to the other fts functions. fts_read() returns a pointer to a structure describing one of the files in the file hierarchy. fts_children() returns a pointer to a linked list of structures, and each one describes one of the files contained in a directory in the hierarchy. iconic behavior, honestly ๐
In general, directories are visited two distinguishable times; in preorder (before any of their descendants are visited) and in postorder (after all of their descendants have been visited). Files are visited once. It is possible to walk the hierarchy โlogicallyโ (visiting the files that symbolic links point to) or physically (visiting the symbolic links themselves), order the walk of the hierarchy or prune and/or revisit portions of the hierarchy ๐ฆ
Two structures (and associated types) are defined in the include file <fts.h>. The first type is FTS, the structure that represents the file hierarchy itself. The second type is FTSENT, the structure that represents a file in the file hierarchy. Normally, an FTSENT structure is returned for every file in the file hierarchy. In this manual page, โfileโ and โFTSENT structureโ are generally interchangeable ๐
The FTSENT structure contains fields describing a file. The structure contains at least the following fields (there are additional fields that should be considered private to the implementation):
typedef struct _ftsent {
unsigned short fts_info; /* flags for FTSENT structure */
char *fts_accpath; /* access path */
char *fts_path; /* root path */
short fts_pathlen; /* strlen(fts_path) +
strlen(fts_name) */
char *fts_name; /* filename */
short fts_namelen; /* strlen(fts_name) */
short fts_level; /* depth (-1 to N) */
int fts_errno; /* file errno */
long fts_number; /* local numeric value */
void *fts_pointer; /* local address value */
struct _ftsent *fts_parent; /* parent directory */
struct _ftsent *fts_link; /* next file structure */
struct _ftsent *fts_cycle; /* cycle structure */
struct stat *fts_statp; /* [l]stat(2) information */
} FTSENT;
These fields are defined as follows:
fts_infoโ One of the following values describing the returnedFTSENTstructure and the file it represents. With the exception of directories without errors (FTS_D), all of these entries are terminal, that is, they will not be revisited, nor will any of their descendants be visited.FTS_Dโ A directory being visited in preorder.FTS_DCโ A directory that causes a cycle in the tree. (Thefts_cyclefield of theFTSENTstructure will be filled in as well.)FTS_DEFAULTโ AnyFTSENTstructure that represents a file type not explicitly described by one of the otherfts_infovalues.FTS_DNRโ A directory which cannot be read. This is an error return, and thefts_errnofield will be set to indicate what caused the error.FTS_DOTโ A file named.or..which was not specified as a filename tofts_open()(seeFTS_SEEDOT).FTS_DPโ A directory being visited in postorder. The contents of theFTSENTstructure will be unchanged from when it was returned in preorder, that is, with thefts_infofield set toFTS_D.FTS_ERRโ This is an error return, and thefts_errnofield will be set to indicate what caused the error.FTS_Fโ A regular file.FTS_NSโ A file for which no[l]stat(2)information was available. The contents of thefts_statpfield are undefined. This is an error return, and thefts_errnofield will be set to indicate what caused the error.FTS_NSOKโ A file for which no[l]stat(2)information was requested. The contents of thefts_statpfield are undefined.FTS_SLโ A symbolic link.FTS_SLNONEโ A symbolic link with a nonexistent target. The contents of thefts_statpfield reference the file characteristic information for the symbolic link itself.
fts_accpathโ A path for accessing the file from the current directory.fts_pathโ The path for the file relative to the root of the traversal. This path contains the path specified tofts_open()as a prefix.fts_pathlenโ The sum of the lengths of the strings referenced byfts_pathandfts_name.fts_nameโ The name of the file.fts_namelenโ The length of the string referenced byfts_name.fts_levelโ The depth of the traversal, numbered from -1 to N, where this file was found. TheFTSENTstructure representing the parent of the starting point (or root) of the traversal is numbered -1, and theFTSENTstructure for the root itself is numbered 0.fts_errnoโ Iffts_children()orfts_read()returns anFTSENTstructure whosefts_infofield is set toFTS_DNR,FTS_ERR, orFTS_NS, thefts_errnofield contains the error number (i.e., theerrnovalue) specifying the error. Otherwise, the contents of thefts_errnofield are undefined.fts_numberโ This field is provided for the use of the application program and is not modified by theftsfunctions. It is initialized to 0.fts_pointerโ This field is provided for the use of the application program and is not modified by theftsfunctions. It is initialized toNULL.fts_parentโ A pointer to theFTSENTstructure referencing the file in the hierarchy immediately above the current file, that is, the directory of which this file is a member. A parent structure for the initial entry point is provided as well, however, only thefts_level,fts_number, andfts_pointerfields are guaranteed to be initialized.fts_linkโ Upon return from thefts_children()function, thefts_linkfield points to the next structure in theNULL-terminated linked list of directory members. Otherwise, the contents of thefts_linkfield are undefined.fts_cycleโ If a directory causes a cycle in the hierarchy (seeFTS_DC), either because of a hard link between two directories, or a symbolic link pointing to a directory, thefts_cyclefield of the structure will point to theFTSENTstructure in the hierarchy that references the same file as the currentFTSENTstructure. Otherwise, the contents of thefts_cyclefield are undefined.fts_statpโ A pointer to[l]stat(2)information for the file.
A single buffer is used for all of the paths of all of the files in the file hierarchy. Therefore, the fts_path and fts_accpath fields are guaranteed to be null-terminated only for the file most recently returned by fts_read(). To use these fields to reference any files represented by other FTSENT structures will require that the path buffer be modified using the information contained in that FTSENT structure's fts_pathlen field. Any such modifications should be undone before further calls to fts_read() are attempted. The fts_name field is always null-terminated.
โจ fts_open() โจ
The fts_open() function takes a pointer to an array of character pointers naming one or more paths which make up a logical file hierarchy to be traversed. The array must be terminated by a null pointer.
There are a number of options, at least one of which (either FTS_LOGICAL or FTS_PHYSICAL) must be specified. The options are selected by ORing the following values:
FTS_LOGICALโ This option causes theftsroutines to returnFTSENTstructures for the targets of symbolic links instead of the symbolic links themselves. If this option is set, the only symbolic links for whichFTSENTstructures are returned to the application are those referencing nonexistent files: thefts_statpfield is obtained viastat(2)with a fallback tolstat(2).FTS_PHYSICALโ This option causes theftsroutines to returnFTSENTstructures for symbolic links themselves instead of the target files they point to. If this option is set,FTSENTstructures for all symbolic links in the hierarchy are returned to the application: thefts_statpfield is obtained vialstat(2).FTS_COMFOLLOWโ This option causes any symbolic link specified as a root path to be followed immediately, as if viaFTS_LOGICAL, regardless of the primary mode.FTS_NOCHDIRโ As a performance optimization, theftsfunctions change directories as they walk the file hierarchy. This has the side-effect that an application cannot rely on being in any particular directory during the traversal. This option turns off this optimization, and theftsfunctions will not change the current directory. Note that applications should not themselves change their current directory and try to access files unlessFTS_NOCHDIRis specified and absolute pathnames were provided as arguments tofts_open().FTS_NOSTATโ so by default, everyFTSENTstructure you get back references the file characteristic information (thefts_statpfield) for each file visited. This option is like "bestie we don't need that" and relaxes the requirement as a performance optimization โ it lets the fts functions set thefts_infofield toFTS_NSOKand leave the contents of thefts_statpfield undefined. ๐FTS_SEEDOTโ by default, unless they're literally specified as path arguments tofts_open(), any files named.or..that show up in the file hierarchy get ignored. This option makes the fts routines returnFTSENTstructures for them anyway. ๐FTS_XDEVโ this one stops fts from descending into directories that have a different device number than the file where the descent began. no crossing the streams bestie โจ
The argument compar() specifies a user-defined function which may be used to order the traversal of the hierarchy. It takes two pointers to pointers to FTSENT structures as arguments and should return a negative value, zero, or a positive value to indicate if the file referenced by its first argument comes before, in any order with respect to, or after, the file referenced by its second argument. The fts_accpath, fts_path, and fts_pathlen fields of the FTSENT structures may never be used in this comparison. If the fts_info field is set to FTS_NS or FTS_NSOK, the fts_statp field may not either. If the compar() argument is NULL, the directory traversal order is in the order listed in path_argv for the root paths, and in the order listed in the directory for everything else. ๐ฆ
๐ fts_read() ๐
The fts_read() function returns a pointer to an FTSENT structure describing a file in the hierarchy. Directories (that are readable and do not cause cycles) are visited at least twice โ once in preorder and once in postorder. All other files are visited at least once. (Hard links between directories that don't cause cycles, or symbolic links to symbolic links, may cause files to be visited more than once โ or directories more than twice. it's giving chaos but ok ๐ธ)
If all the members of the hierarchy have been returned, fts_read() returns NULL and sets errno to 0. If an error unrelated to a file in the hierarchy occurs, fts_read() returns NULL and sets errno to indicate the error. If an error related to a returned file occurs, a pointer to an FTSENT structure is returned, and errno may or may not have been set (see fts_info). ๐ญ
The FTSENT structures returned by fts_read() may be overwritten after a call to fts_close() on the same file hierarchy stream, or, after a call to fts_read() on the same file hierarchy stream โ unless they represent a file of type directory, in which case they will not be overwritten until after a call to fts_read() after the FTSENT structure has been returned by the function fts_read() in postorder. ๐
๐ง fts_children() ๐ง
The fts_children() function returns a pointer to an FTSENT structure describing the first entry in a NULL-terminated linked list of the files in the directory represented by the FTSENT structure most recently returned by fts_read(). The list is linked through the fts_link field of the FTSENT structure, and is ordered by the user-specified comparison function, if any. Repeated calls to fts_children() will re-create this linked list. ๐ฅ
As a special case, if fts_read() has not yet been called for a hierarchy, fts_children() will return a pointer to the files in the logical directory specified to fts_open() โ that is, the arguments specified to fts_open(). Otherwise, if the FTSENT structure most recently returned by fts_read() is not a directory being visited in preorder, or the directory does not contain any files, fts_children() returns NULL and sets errno to zero. If an error occurs, fts_children() returns NULL and sets errno to indicate the error. ๐
The FTSENT structures returned by fts_children() may be overwritten after a call to fts_children(), fts_close(), or fts_read() on the same file hierarchy stream.
The instr argument is either zero or the following value:
FTS_NAMEONLYโ only the names of the files are needed, bestie. The contents of all the fields in the returned linked list of structures are undefined, with the exception of thefts_nameandfts_namelenfields. ๐
๐ fts_set() ๐
The function fts_set() allows the user application to determine further processing for the file f of the stream ftsp. The fts_set() function returns 0 on success, and -1 if an error occurs.
The instr argument is either 0 (meaning "do nothing" โ iconic, honestly) or one of the following values:
FTS_AGAINโ revisit the file; any file type may be revisited. The next call tofts_read()will return the referenced file. Thefts_statandfts_infofields of the structure will be reinitialized at that time, but no other fields will have been changed. This option is meaningful only for the most recently returned file fromfts_read(). Normal use is for postorder directory visits, where it causes the directory to be revisited (in both preorder and postorder) as well as all of its descendants. ๐ซถFTS_FOLLOWโ the referenced file must be a symbolic link. If the referenced file is the one most recently returned byfts_read(), the next call tofts_read()returns the file with thefts_infoandfts_statpfields reinitialized to reflect the target of the symbolic link instead of the symbolic link itself. If the file is one of those most recently returned byfts_children(), thefts_infoandfts_statpfields of the structure, when returned byfts_read(), will reflect the target of the symbolic link instead of the symbolic link itself. In either case, if the target of the symbolic link does not exist, the fields of the returned structure will be unchanged and thefts_infofield will be set toFTS_SLNONE. ๐ฑ
If the target of the link is a directory, the preorder return, followed by the return of all of its descendants, followed by a postorder return, is done. โจ
FTS_SKIPโ no descendants of this file are visited. The file may be one of those most recently returned by eitherfts_children()orfts_read(). byeee ๐ธ
๐ fts_close() ๐
The fts_close() function closes the file hierarchy stream referred to by ftsp and restores the current directory to the directory from which fts_open() was called to open ftsp. The fts_close() function returns 0 on success, and -1 if an error occurs.
๐ญ ERRORS ๐ญ
The function fts_open() may fail and set errno for any of the errors specified for open(2) and malloc(3).
In addition, fts_open() may fail and set errno as follows:
ENOENTโ any element ofpath_argvwas an empty string. not the empty string bestie ๐
The function fts_close() may fail and set errno for any of the errors specified for chdir(2) and close(2).
The functions fts_read() and fts_children() may fail and set errno for any of the errors specified for chdir(2), malloc(3), opendir(3), readdir(3), and [l]stat(2).
In addition, fts_children(), fts_open(), and fts_set() may fail and set errno as follows:
EINVALโoptionsorinstrwas invalid. ๐
๐ ATTRIBUTES ๐
For an explanation of the terms used in this section, see attributes(7).
fts_open(),fts_set(),fts_close()โ Thread safety: MT-Safe โfts_read(),fts_children()โ Thread safety: MT-Unsafe โ ๏ธ
โญ STANDARDS โญ
None. (yeah. nothing. it's a whole vibe, it's just not a standard one ๐ )
๐ฆ HISTORY ๐ฆ
glibc 2. 4.4BSD.
๐ BUGS ๐
Before glibc 2.23, all of the APIs described in this man page are not safe when compiling a program using the LFS APIs (e.g., when compiling with -D_FILE_OFFSET_BITS=64). so like, if you're time travelling, be careful. ๐ฐ๏ธ
๐ธ SEE ALSO ๐ธ
find(1), chdir(2), lstat(2), stat(2), ftw(3), qsort(3)
and that's the whole page bestie, we ate and left no crumbs ๐ this one's from Linux man-pages 6.20, dated 2025-09-21 โ go forth and traverse your file hierarchies with main character energy, ily ๐ซถโจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: BSD-4-Clause-UC
- Copyright 1989-1994, The Regents of the University of California
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms