The cd command in Linux
The cd (change directory) command moves you around the filesystem. Together with ls and pwd it forms the basic navigation toolkit of the shell. cd is a shell built-in rather than a separate program, because it needs to change the state of the shell itself.
How cd works
Every running process carries its own "current working directory" as part of its kernel-side state, and relative paths are resolved against it. cd works by asking the kernel to change that state via the chdir() system call.
This explains the one genuinely odd thing about cd: it must be a shell built-in, not a program. If cd were an external binary, it would run as a child process, change its own working directory, and exit — leaving your shell exactly where it was. Only the shell can move the shell. The same logic explains why a script cannot change the directory of the terminal that launched it: the script is a child process, and working directories flow down to children at launch, never back up.
Syntax
cd [DIRECTORY] Common options
| Option | What it does |
|---|---|
(none) | With no argument, cd takes you to your home directory. |
- | cd - returns to the previous directory you were in (and prints it). |
.. | Not an option but a special path: the parent of the current directory. |
~ | Expands to your home directory, so cd ~/projects works from anywhere. |
How to use cd: examples
$ cd documents Relative path: enter the documents folder inside the current directory.
$ cd /var/log Absolute path: starts with / and works no matter where you currently are.
$ cd .. Go up one level to the parent directory.
$ cd ../.. Go up two levels.
$ cd With no argument, jump straight to your home directory from anywhere.
$ cd ~/projects/web The tilde expands to your home directory, making this an absolute path in disguise.
$ cd - Switch back to wherever you were before the last cd. Running it repeatedly toggles between two directories.
Real-world use cases for cd
Bouncing between two work directories
Editing code in ~/projects/app and checking logs in /var/log/app means constant switching. cd - toggles between the two locations with three keystrokes — no retyping paths, no aliases needed.
Scripting safely
Deployment scripts should never assume where they are started from. cd "$(dirname "$0")" at the top of a bash script moves to the script's own directory, making every relative path in it reliable.
Try cd yourself
This is a live sandbox with a small filesystem (documents/, notes.txt,
backup.sh…). Nothing you do here can break anything — experiment freely.
Try: cd documents
Pro tips and common mistakes
- Tab completion makes long paths trivial: type cd pro<Tab>we<Tab> and the shell expands each segment.
- cd with no arguments always goes home — faster than typing cd ~ or cd /home/you.
- If a directory name has spaces, quote it (cd "My Documents") or let Tab completion escape it for you.
- CDPATH is an underused gem: export CDPATH=.:~/projects lets you cd app from anywhere if ~/projects/app exists.
Frequently asked questions about cd
What is the difference between a relative and an absolute path?
An absolute path starts with / and describes the full route from the root of the filesystem (e.g. /home/user/documents). A relative path is interpreted starting from your current directory (e.g. documents or ../photos).
How do I go back to the previous directory?
Run cd - (cd followed by a dash). The shell remembers the last directory you were in and switches back to it.
Why does cd .. have a space?
cd is the command and .. is its argument, so they must be separated by a space. cd.. without the space is not a valid command on Linux.
Related commands
List the contents of a directory, with options for hidden files, long format, sorting and more.
pwdShow the absolute path of the directory you are currently in.
mkdirCreate one or many directories, including whole nested paths with -p.
findLocate files anywhere in a directory tree by name, type, size, date — and act on them.