Linux chdir function

Updated: 11/06/2021 by Computer Hope
chdir command

On Unix-like operating systems, chdir is the system call for changing the current working directory.

This page covers the GNU/Linux version of chdir.

Description

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

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.

Errors

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

EACCES Search permission is denied for one of the components of path.
EFAULT path points outside the accessible address space.
EIO An I/O error occurred.
ELOOP Too many symbolic links were encountered in resolving path.
ENAMETOOLONG path is too long.
ENOENT The file does not exist.
ENOMEM Insufficient kernel memory was available.
ENOTDIR A component of path is not a directory.

Notes

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.