You are here: >

Linux and Unix chdir function

chdir commandAbout chdir
chdir syntax
Related commands
Linux and Unix main page

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.

Description

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.

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.