Home

fts

fts is a set of functions in the C standard library used to traverse file hierarchies in a controlled, portable way. Originating in BSD Unix and adopted in POSIX, the interface provides a depth-first walk of directories and their contents, returning information about each filesystem entry through a structured type. It is commonly used by utilities and programs that need to process many files recursively, with fine-grained control over how symlinks, directories, and device boundaries are handled.

The core API consists of a few functions. fts_open takes an array of pathnames, a set of

FTS and FTSENT provide fields that describe each entry, including the path, depth level, and traversal information.

Options control traversal behavior. For example, FTS_PHYSICAL performs a non-following of symbolic links, while FTS_LOGICAL follows

On systems without a native fts implementation, similar functionality may be provided by libraries or alternatives

option
flags,
and
an
optional
comparison
function,
and
returns
a
handle
to
an
internal
traversal
state.
fts_read
returns
the
next
entry
in
the
traversal
as
an
FTSENT
object,
which
contains
details
about
the
current
path,
its
depth
in
the
tree,
and
the
type
or
status
of
the
entry.
fts_children
can
be
used
to
obtain
the
list
of
children
for
a
directory
node,
and
fts_close
releases
resources
when
traversal
is
finished.
A
companion
function,
fts_options,
allows
querying
or
modifying
traversal
behavior.
The
traversal
returns
entries
with
an
fts_info
value
that
encodes
the
kind
of
entry,
such
as
a
directory,
a
regular
file,
or
a
symbolic
link,
as
well
as
error
states
or
special
conditions.
This
information
enables
callers
to
implement
custom
logic
for
processing,
skipping,
or
deleting
files.
them.
FTS_XDEV
prevents
descending
onto
other
mounted
filesystems.
FTS_SEEDOT
includes
dot
entries
(like
.
and
..)
in
the
walk,
and
FTS_NOCHDIR
prevents
changing
the
current
working
directory
during
traversal.
These
controls
provide
robust,
portable
file-system
operations
across
Unix-like
systems.
such
as
nftw
or
ftw,
or
via
libbsd
on
Linux.