Linux chdir function

Updated: 04/26/2017 by Computer Hope

About chdir

chdir is the system function for changing the current working directory.

On some systems, chdir may also be an alias for the shell command cd.


chdir changes the current working directory of the calling process to the directory specified in path.

chdir syntax

#include <unistd.h>

int chdir(const char *path);

Return Value

On success, zero (0) is returned. On an error, -1 is returned, and errno is set appropriately.


Depending on the filesystem, other errors can be returned. The more general errors for chdir are listed below:


Search permission is denied for one of the components of path.


path points outside the accessible address space.


An I/O error occurred.


Too many symbolic links were encountered in resolving path.


path is too long.


The file does not exist.


Insufficient kernel memory was available.


A component of path is not a directory.


The current working directory is the starting point for interpreting relative pathnames (those not starting with '/').

A child process created via fork inherits its parent's current working directory. The current working directory is left unchanged by execve.

cd — Change the working directory.
chroot — Run a command or shell from another directory, and treat that directory as root.
ls — List the contents of a directory or directories.