The cd (change directory) command is the most basic navigation tool in Unix-like shells, but it has several tricks that can save you significant time. This article covers 12 practical cd tricks, from quick shortcuts to advanced directory stacks, that work in Bash, Zsh, and most other shells.
Background: What is the cd Command?
The cd command is a shell built-in (not a separate program) that changes the current working directory. It has been part of Unix since the early 1970s and is present in every Unix-like operating system, including Linux, macOS, and Windows Subsystem for Linux (WSL). The command takes a single argument: the path to the directory you want to move to. If you run cd with no arguments, it takes you to your home directory.
Understanding a few core concepts makes the tricks below easier to grasp:
- Current directory: The folder your shell is currently “in”. You can see it with
pwd. - Absolute path: A path that starts from the root (
/), e.g.,/home/user/projects. - Relative path: A path that starts from your current directory, e.g.,
../docs(go up one level, then intodocs). - Home directory: Your personal folder, usually
/home/usernameon Linux or/Users/usernameon macOS. It is represented by the tilde (~).
How It Works: The Basics You Need to Know
When you type a path after cd, the shell resolves it relative to your current directory unless you start with / (absolute) or ~ (home). For example:
cd Documents # goes to ./Documents
cd /var/log # absolute path
cd ~/Downloads # home directory, then Downloads
The shell also supports special dot entries:
.– the current directory..– the parent directory~– your home directory~user– another user’s home directory (if you have permission)-– the previous directory (see trick #1)
12 cd Command Tricks
Here are the most useful tricks, each with a concrete example.
1. Go to the Previous Directory with cd -
Typing cd - takes you back to the directory you were in before the last cd. This is a huge time-saver when you are toggling between two folders. It also prints the path so you know where you are.
cd /var/log
cd /home/user/projects
cd - # now in /var/log
cd - # now in /home/user/projects
2. Use cd ~ to Jump Home
Just typing cd (no arguments) or cd ~ takes you to your home directory. This is faster than typing the full path.
3. Use cd .. to Go Up One Level
To go up one directory, use cd ... To go up two levels, use cd ../... You can chain as many .. as you need.
cd /home/user/projects/website/src
cd ../.. # now in /home/user/projects
4. Use cd with Tab Completion
Press Tab after typing part of a directory name to auto-complete it. If there are multiple matches, press Tab twice to see all options. This works in every modern shell and saves you from typos.
5. Use cd with Wildcards
You can use wildcards like * and ? in the path. For example, if you have a directory named project-final-v2, you can type cd proj* and the shell will expand it. Be careful: if multiple directories match, the shell will use the first one alphabetically.
6. Use pushd and popd to Manage a Directory Stack
The pushd command changes directory and also stores the old one on a stack. popd returns to the most recent stored directory. This is useful when you need to jump to a temporary location and then come back.
pushd /tmp # go to /tmp, stack: /home/user
pushd /var/log # go to /var/log, stack: /home/user /tmp
popd # back to /tmp, stack: /home/user
popd # back to /home/user, stack: empty
7. Use cd with Environment Variables
You can use environment variables in the path. For example, cd $HOME or cd $OLDPWD (the previous directory). This is especially useful in scripts.
8. Use cd with Command Substitution
You can use the output of a command as the path. For example, cd $(find . -type d -name "src") would go to the first src directory found. This is advanced but powerful.
9. Use cd with OLDPWD in Scripts
The shell automatically sets the variable OLDPWD to the previous directory. You can use it in scripts: cd $OLDPWD is equivalent to cd -.
10. Use cd to Go to a Directory with Spaces
If a directory has spaces, you must quote the path or escape the spaces with a backslash. For example:
cd "My Documents" # correct
cd My\ Documents # also correct
cd My Documents # wrong: tries to go to "My" then "Documents"
11. Use cd with ~ for Other Users
If you have permission, you can go to another user’s home with cd ~otheruser. This is handy for system administrators.
12. Use cd to Fix a Typo with cd and fc
If you type a long path and make a typo, you can use the fc command to edit the previous command. But a simpler trick is to press Up to recall the command, then use Ctrl+W to delete the last word and retype it.
Common Problems and How to Solve Them
Here are the most common issues people run into with cd:
- “No such file or directory” – The path is wrong. Check for typos, spaces, and case sensitivity.
- “Permission denied” – You don’t have execute permission on the directory. Use
ls -lto check permissions. - “Not a directory” – You tried to
cdinto a file. Usels -lto confirm it’s a directory. - Stuck in a symlink loop – If you have symlinks pointing to each other,
cdmay loop. Usecd -Pto resolve symlinks to physical paths.
Comparing cd with Other Navigation Tools
While cd is the standard, there are alternatives:
- z / autojump – These tools learn your most-used directories and let you jump with fuzzy matching (e.g.,
z proj). - pushd/popd – As shown above, they manage a stack.
- cdpath – You can set the
CDPATHenvironment variable to a list of directories thatcdsearches if you don’t give an absolute path.
Safety and Best Practices
Using cd is safe, but a few habits prevent mistakes:
- Always double-check the path before pressing Enter, especially as root.
- Use
pwdto confirm where you are before running destructive commands. - In scripts, use
cdwith error checking:cd /path || exit 1. - Be careful with wildcards – they can expand to unexpected directories.
Frequently Asked Questions
How do I go back to the last directory I was in?
Type cd -. It toggles between your current and previous directory. The shell stores the previous directory in the OLDPWD variable.
What does cd do with no arguments?
It changes to your home directory (the directory in the HOME environment variable). This is equivalent to cd ~.
How do I go up multiple directories at once?
Use cd ../.. to go up two levels, or cd ../../.. for three. You can also use cd .. repeatedly.
Why does cd not work in a script?
In a script, cd changes the directory for that script’s process, not your interactive shell. If you want the change to persist after the script ends, you need to source the script (e.g., source script.sh) or use a function.
Can I use cd with a variable?
Yes. For example, DIR="/var/log" then cd $DIR. To be safe, quote it: cd "$DIR" to handle spaces.
What is the difference between cd and pushd?
cd simply changes the directory. pushd changes the directory and also saves the old one on a stack, so you can later return with popd. This is useful for temporary detours.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Backup Not Enough Space? Fix It Now200.00 GB
- Approximate Match Lookup: Conditional Formatting
- GST on Delivery Charges in India: E-Commerce Rules
- NFC Reader on Your Phone: 9 Things You’re Not Doing
- Smart TV DNS Settings: Why They Change & How to Fix
- RabbitMQ Hosting for Startups: Managed vs Self-Hosted