LinuxCommandLibrary

cd

Change the current working directory

TLDR

Go to the specified directory

$ cd [path/to/directory]
copy

Go up to the parent of the current directory
$ cd ..
copy

Go to the home directory of the current user
$ cd
copy

Go to the home directory of the specified user
$ cd ~[username]
copy

Go to the previously chosen directory
$ cd -
copy

Go to the root directory
$ cd /
copy

SYNOPSIS

cd [OPTION]... [DIRECTORY]

PARAMETERS

-L, --logical
    Use logical path resolution (default; follows symlinks in $PWD)

-P, --physical
    Use physical path resolution (ignores symlinks)

--help
    Display usage help and exit

--version
    Output version information and exit

DESCRIPTION

cd is a fundamental shell builtin command used to navigate the filesystem by changing the current working directory (CWD) of the invoking shell process.

It supports absolute and relative paths, with special handling for home directories via ~ or $HOME, previous directory via - (referencing $OLDPWD), and directory name completion influenced by the CDPATH environment variable.

When invoked without arguments, cd switches to the user's home directory. Symbolic links are followed by default (-L or logical mode), but -P resolves physical paths, ignoring symlinks. This affects commands like pwd, which reflect the new CWD.

Unlike external commands, cd cannot be executed directly in subshells or scripts without impacting the parent shell, as directory changes are process-specific. It's essential for scripting workflows, interactive sessions, and shell functions. Most POSIX-compliant shells (bash, zsh, dash) implement it similarly, with minor extensions like zsh's autocd.

CAVEATS

Builtin only affects current shell; subshells/scripts need source or . to propagate changes. Fails silently if target doesn't exist or lacks permissions (non-zero exit). CDPATH may cause unexpected jumps.

SPECIAL DIRECTORY CASES

No arg: $HOME
-: $OLDPWD (previous dir)
~ or ~user: User home dirs (tilde expansion)
CDPATH: Colon-separated search paths for relative dirs

HISTORY

Originated in Version 7 Unix Bourne shell (1979) as core navigation tool. Evolved with POSIX.1-1988 standardization; modern shells add features like hash table integration for speed.

SEE ALSO

pwd(1), pushd(1), popd(1)

Copied to clipboard