>_cmd.script

cd

Change the shell's current working directory

Directories

By CMD Script Team · 4 min read · Last updated

SYNTAX
cd [OPTIONS] [DIRECTORY]

Options

Command options and flags
FlagDescription
-Switch to the previous working directory ($OLDPWD)
~Refers to the current user's home directory (used as an argument, e.g. cd ~)
(no argument)With no argument, changes to the current user's home directory
-PChange to the physical directory, resolving symlinks instead of keeping the logical path
..Refers to the parent of the current directory (used as an argument, e.g. cd ..)

Distribution compatibility

  • Ubuntu
  • Debian
  • Fedora
  • Arch
  • macOS

What it does

cd ("change directory") is a shell builtin that changes the current shell session's working directory. Because it modifies the state of the running shell process itself, it cannot be implemented as an external program — every shell (bash, zsh, sh, etc.) implements it natively.

Beginner examples

  • cd /var/log — change to an absolute path
  • cd project/src — change to a path relative to the current directory
  • cd .. — move up one directory level to the parent
  • cd (no argument) or cd ~ — jump straight to your home directory
  • cd - — jump back to the previous directory you were in
cd /var/log

Advanced examples

  • Toggle rapidly between two directories: cd - switches to $OLDPWD; running it again switches back, useful when comparing files in two locations.
  • Chain directory changes with a command in one line: cd /tmp && ls -la (runs ls only if cd succeeds).
  • Use cd -P to jump into a symlinked directory but land on its real, resolved path rather than keeping the symlink in the logical path shown by a later pwd.
  • Combine with command substitution to jump to a computed location: cd "$(dirname "$0")" inside a script, to move to the directory containing the running script.
cd /tmp && tar xzf archive.tar.gz && cd -

Common mistakes

  • Writing cd /some/path; command and not checking whether cd actually succeeded — if the path doesn't exist, the script keeps running command from the wrong directory. Use cd /some/path && command or check the exit status explicitly.
  • Forgetting that cd only affects the current shell session — a cd inside a subshell, script, or $(...) command substitution does not change the directory of the parent/calling shell.
  • Confusing cd .. (up one level) with cd - (back to the previous directory) — they do very different things and are easy to typo into each other.
  • Assuming an unquoted path with spaces will work: cd My Documents fails; it needs to be cd "My Documents".

Tips

  • Use cd - as a quick toggle when working between two directories, instead of typing out full paths repeatedly.
  • In scripts, always chain cd with && (or check its exit code) before running subsequent commands, since a failed cd that's ignored can cause commands to run in the wrong directory.
  • Many shells support directory stacks (pushd/popd) for navigating between more than two directories, which goes beyond what plain cd - offers.

Best practices

  • Always guard directory-dependent script logic with cd /path && rest_of_script (or an explicit exit-status check), rather than assuming cd succeeded.
  • Prefer absolute paths in scripts and automation over relative cd targets, to avoid behavior that depends on where the script happens to be invoked from.
  • Use cd "$(dirname "$0")" at the top of a shell script that needs to reference files relative to its own location, rather than assuming a fixed working directory.

Try it yourself

A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.

Real-world use cases

  • Quickly bouncing between a project's source directory and its build/output directory using cd -.
  • Writing deployment scripts that cd into a target directory before running build or install steps, always checking the cd succeeded first.
  • Navigating a deep directory structure during debugging, using cd .. repeatedly or cd ../../.. to jump back several levels at once.

Common interview questions

  • Why is cd a shell builtin instead of an external program? Because it must change the working directory of the current shell process itself; an external program runs in its own process and any directory change it makes wouldn't affect its parent shell.
  • What does cd - do? It changes to the previous working directory (stored in $OLDPWD) and prints the path switched to, effectively toggling between two directories on repeated use.
  • Why should cd be chained with && in scripts rather than run as a separate statement? Because if the cd fails (e.g. the directory doesn't exist) and the script doesn't check for that, subsequent commands silently run in the wrong directory, which can be dangerous for destructive operations.

Frequently Asked Questions

What does cd - do?

It changes back to whatever directory you were in before your most recent cd, using the $OLDPWD shell variable, and prints the path it switched to. Running cd - twice toggles you back and forth between two directories.

What's the difference between cd, cd ~, and cd with no argument?

They're equivalent — all three change to the current user's home directory ($HOME). cd ~ is explicit about it; bare cd relies on the shell defaulting to $HOME when no argument is given.

Is cd a builtin or an external program?

cd must be a shell builtin — it changes the working directory of the current shell process itself, and no external program can alter its parent shell's working directory (a child process's cwd change wouldn't propagate back to the shell that spawned it).

How do I go up multiple directory levels at once?

Chain .. with slashes, e.g. cd ../../.. goes up three levels. There's no single-flag shortcut for 'n levels up' in POSIX cd; some shells/frameworks add aliases like .... for convenience.

Cheat sheet

Download a quick-reference cheat sheet for cd.