Home

chdir

Chdir, short for change directory, is a function used in many operating system interfaces to modify the current working directory of the calling process. It is part of the POSIX standard and is exposed in C libraries as a system or library call; Windows provides a similar function such as _chdir in its C runtime. The primary purpose is to switch the process’s perspective of where relative paths begin.

The typical signature is int chdir(const char *path). On success, it returns 0; on error, it returns

The effect of chdir is confined to the process that invokes it. It changes the process’s own

High-level language wrappers commonly expose chdir-like functionality. For example, Python’s os.chdir() and other languages offer similar

-1
and
sets
errno
to
indicate
the
failure
reason,
such
as
ENOENT
(no
such
file
or
directory),
ENOTDIR
(not
a
directory),
or
EACCES
(permission
denied).
The
path
may
be
absolute
or
relative;
when
relative,
it
is
interpreted
with
respect
to
the
process’s
current
working
directory.
The
process
must
have
the
necessary
execute/search
permissions
for
each
component
of
the
path.
working
directory,
and
this
change
is
inherited
by
child
processes
created
afterward.
It
does
not
alter
the
working
directory
of
the
calling
process’s
parent
or
sibling
processes.
In
interactive
shells,
changing
directories
is
typically
implemented
as
a
built-in
command
(for
example,
cd)
so
that
the
shell
itself
reflects
the
new
directory.
interfaces
that
internally
call
the
underlying
OS
function.
Handling
errors
and
permissions
is
a
common
part
of
using
chdir
across
languages
and
platforms.