cd Command
Beginner File Management man(1)Change the current working directory
👁 10 views
📅 Updated: Mar 15, 2026
SYNTAX
cd [DIRECTORY]
What Does cd Do?
The cd (change directory) command is a shell built-in that changes the current working directory. It is the most fundamental navigation command in Linux, used constantly in daily operations.
cd supports absolute paths (starting from /), relative paths (from current location), and several shortcuts like ~ for home directory, - for previous directory, and .. for parent directory. Since cd is a shell built-in, it executes within the current shell process rather than spawning a new one.
Understanding directory navigation is essential for all Linux work. cd pairs naturally with ls and pwd to explore the filesystem. Environment variables like $HOME and $OLDPWD are used internally by cd to resolve paths.
cd supports absolute paths (starting from /), relative paths (from current location), and several shortcuts like ~ for home directory, - for previous directory, and .. for parent directory. Since cd is a shell built-in, it executes within the current shell process rather than spawning a new one.
Understanding directory navigation is essential for all Linux work. cd pairs naturally with ls and pwd to explore the filesystem. Environment variables like $HOME and $OLDPWD are used internally by cd to resolve paths.
Options & Flags
| Option | Description | Example |
|---|---|---|
| - | Switch to the previous directory ($OLDPWD) | cd - |
| ~ | Navigate to the home directory | cd ~ |
| .. | Move up one directory level | cd .. |
| -P | Use physical directory structure (resolve symlinks) | cd -P /var/log |
| -L | Follow symbolic links (default behavior) | cd -L /var/log |
| ~username | Navigate to another user's home directory | cd ~www-data |
Practical Examples
#1 Navigate to an absolute path
Changes to the Nginx log directory using an absolute path.
$ cd /var/log/nginx#2 Move up two directory levels
Moves up two levels in the directory hierarchy.
$ cd ../..#3 Return to home directory
Shortcut to return to your home directory. Just typing cd without arguments does the same.
$ cd ~#4 Switch between two directories
Toggles between the current and previous directory. Shows the new path.
$ cd -
Output:
/home/user/projects
#5 Navigate using environment variable
Uses the HOME environment variable to build the path.
$ cd $HOME/Documents#6 Handle directory names with spaces
Quotes are needed when directory names contain spaces or special characters.
$ cd "My Documents"#7 Follow physical path (resolve symlinks)
Resolves any symbolic links in the path, showing the actual directory.
$ cd -P /var/www#8 Navigate to another user home
Changes to the home directory of the deploy user (requires permissions).
$ cd ~deployTips & Best Practices
Use tab completion: Press Tab while typing a path to auto-complete directory names. Double-tap Tab to see all matching options.
CDPATH variable: Set CDPATH to frequently used parent directories: export CDPATH=.:~:/var/www — then cd mysite works from anywhere if /var/www/mysite exists.
pushd/popd for complex navigation: Use pushd/popd instead of cd to maintain a directory stack. pushd /var/log saves your current location, and popd returns to it.
Frequently Asked Questions
Why does cd not work in a script?
cd in a script only affects the subshell running the script, not your current shell. To have the effect persist, source the script with: source script.sh or . script.sh
How do I go back to my previous directory?
Use cd - to toggle back to the last directory you were in. The previous path is stored in the $OLDPWD variable.
What is the difference between cd ~ and just cd?
Both go to your home directory. cd without arguments defaults to $HOME. cd ~ explicitly expands the tilde to your home directory. They are functionally identical.
Related Commands
More File Management Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →