cd
Change the shell's current working directory
By CMD Script Team · 4 min read · Last updated
cd [OPTIONS] [DIRECTORY]Options
| Flag | Description |
|---|---|
- | 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 |
-P | Change 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 pathcd project/src— change to a path relative to the current directorycd ..— move up one directory level to the parentcd(no argument) orcd ~— jump straight to your home directorycd -— 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(runslsonly ifcdsucceeds). - Use
cd -Pto jump into a symlinked directory but land on its real, resolved path rather than keeping the symlink in the logical path shown by a laterpwd. - 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; commandand not checking whethercdactually succeeded — if the path doesn't exist, the script keeps runningcommandfrom the wrong directory. Usecd /some/path && commandor check the exit status explicitly. - Forgetting that
cdonly affects the current shell session — acdinside a subshell, script, or$(...)command substitution does not change the directory of the parent/calling shell. - Confusing
cd ..(up one level) withcd -(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 Documentsfails; it needs to becd "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
cdwith&&(or check its exit code) before running subsequent commands, since a failedcdthat'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 plaincd -offers.
Best practices
- Always guard directory-dependent script logic with
cd /path && rest_of_script(or an explicit exit-status check), rather than assumingcdsucceeded. - Prefer absolute paths in scripts and automation over relative
cdtargets, 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
cdinto a target directory before running build or install steps, always checking thecdsucceeded first. - Navigating a deep directory structure during debugging, using
cd ..repeatedly orcd ../../..to jump back several levels at once.
Common interview questions
- Why is
cda 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
cdbe chained with&&in scripts rather than run as a separate statement? Because if thecdfails (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.