Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Linux cd command

Reference for the Bash cd builtin: change the working directory, use ~, - and .., understand CDPATH, -P and -L, and avoid common mistakes in scripts.

cd changes the current working directory of the shell. It is a shell builtin, not a program, and it has to be: a separate process could only change its own directory, never the one of the shell that started it.

With no argument cd goes to $HOME. With - it returns to the previous directory, which is stored in OLDPWD. Relative paths are resolved from the current directory, and .. means the parent.

Terminal
cd [-L|-P] [directory]
cd -

Try it

Bash Moving around

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Failure, subshells and symlinks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
cd [-L|-P[-e]] [directory]
Name Description
directory Target directory. If omitted, $HOME is used. - means $OLDPWD.
-L Follow symbolic links logically (default): .. removes the last path component as written.
-P Resolve symlinks first, so .. is the real parent directory.
-e With -P, return status 1 if the working directory cannot be determined afterwards.

Returns: 0 if the directory changed, non-zero otherwise (missing directory, not a directory, no execute permission, HOME unset).

Variables set: PWD (new directory) and OLDPWD (previous one).

Usage notes

  • Execute permission is required. To enter a directory you need the x bit on it, not only r. Without x, cd fails with "Permission denied".
  • cd - prints the new directory to standard output (it is the one case where cd is not silent), which is handy but unwanted in scripts. Redirect it with cd - >/dev/null.
  • CDPATH works like PATH for cd: relative names are looked up in each listed directory. It is convenient interactively, but exporting it surprises scripts. If CDPATH is set, cd may print the chosen directory.
  • Check the result in scripts. Write cd "$dir" || exit 1. Otherwise a failed cd lets the rest of the script run in the wrong place, for example deleting files in the wrong directory.
  • Quote the argument. cd $dir breaks on spaces and glob characters; cd "$dir" does not. A name that starts with a dash needs -- (cd -- -weird).
  • Tilde expansion: cd ~ is home, cd ~user is another user's home, cd ~+ is the current directory, and cd ~- is the previous one.
  • Subshell trick: ( cd dir && command ) runs a command elsewhere without changing the directory of the shell afterwards.
  • shopt -s autocd lets you type a directory name alone to change into it. shopt -s cdspell corrects small typos interactively.
  • A directory removed under you: if the current directory is deleted, pwd still prints the old name but relative operations fail until you cd somewhere valid.