cd
Overview
cd (change directory) sets the shell’s current working directory. It is a shell builtin, not an external program — each shell (bash, zsh, dash) implements it. Directory changes never affect other already-running processes; only the current shell (and its children) see the new cwd.
Syntax
cd [-L|[-P [-e]] [-@]] [directory]
cd -Common Options / operands
| Option / form | Description |
|---|---|
-L |
Follow logical path (default); retain symlink components |
-P |
Physical structure; resolve symlinks when determining path |
-e |
With -P, exit non-zero if physical path cannot be determined |
- |
Switch to previous directory ($OLDPWD) |
~ or empty |
Home directory ($HOME) |
~user |
Home of user |
.. / . |
Parent / current |
-- |
End of options (path starts with -) |
Key Use Cases
- Navigate the filesystem in interactive shells
- Anchor scripts to a known directory
- Toggle between two locations with
cd - - Control symlink resolution (
-Lvs-P) - Combine with
pushd/popdfor a directory stack
Examples with Explanations
Previous directory
cd /etc
cd /var/log
cd - # back to /etc
cd - # toggle again$OLDPWD holds the previous directory; cd - prints the new path in many shells.
Symlink behavior
mkdir -p /tmp/real/sub
ln -sfn /tmp/real /tmp/link
cd /tmp/link/sub
pwd -L # /tmp/link/sub
pwd -P # /tmp/real/sub
cd -P /tmp/link/sub
pwd # physicalPaths that start with a dash
mkdir -- -weird
cd -- -weirdScript: run relative to script location
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$(readlink -f "$0")")"
pwd
# now relative paths are stable regardless of caller cwdScript: require a directory
cd /srv/app || { echo "missing /srv/app" >&2; exit 1; }pushd / popd alternative workflow
pushd /var/log
pushd /etc/systemd/system
dirs -v
popd
popdcd alone does not maintain a stack; use pushd/popd when you need one.
One-liners
cd /usr/local/bin && pwd
cd ~alice
cd "$(dirname "$(realpath ./config.yml)")"
cd "$(mktemp -d)" # disposable workspaceUnderstanding success and failure
- Success: no output (except
cd -often prints the path). - Common errors: permission denied, no such file, not a directory, deleted cwd.
- Exit status is non-zero on failure — always check in scripts (
cd dir || exit 1).
Notes / Pitfalls
cdin a subshell does not change the parent:(cd /tmp); pwdstill shows the old path.- Aliases like
alias cd='cd -P'change defaults for interactive use only. CDPATH(if set) makescd somesearch a list of base directories — surprising in scripts; unset or avoid relying on it in portable scripts.- Broken symlinks and removed directories: physical
cd -Pfails more honestly. zshauto-cd(typing a directory name) is a shell feature, not POSIXcd.
2026-relevant notes
- Containers and distroboxes: know whether you are on the host or in a named mount namespace before bulk relative operations.
- Network homes (
$HOMEon NFS) make frequentcdacross mounts latency-sensitive; stay local when compiling. - Prefer explicit absolute anchors in automation (
cd /opt/app) over long chains ofcd ...
Additional Resources
help cd(bash)- Bash manual — Bourne Shell Builtins