Beyond the Basics of cd
Every Linux user learns cd in their first terminal session. But most never discover its full power. This guide covers techniques that will dramatically speed up your navigation.
Essential Shortcuts
# Go to home directory (three equivalent ways)
cd
cd ~
cd $HOME
# Go to previous directory (toggle between two)
cd -
# Go up one level
cd ..
# Go up multiple levels
cd ../..
cd ../../..
# Go to root
cd /
Advanced Navigation Patterns
Using CDPATH
Set CDPATH to create shortcut base directories:
# Add to ~/.bashrc or ~/.zshrc
export CDPATH=".:~:~/projects:/var/www"
# Now you can cd to any subdirectory of these paths
cd myproject # Finds ~/projects/myproject automatically
cd html # Finds /var/www/html automatically
Directory Stack with pushd/popd
# Push current directory and change
pushd /etc/nginx
# ... do work ...
# Push another
pushd /var/log
# ... check logs ...
# View the stack
dirs -v
# 0 /var/log
# 1 /etc/nginx
# 2 ~/projects
# Pop back to previous
popd # Returns to /etc/nginx
popd # Returns to ~/projects
Brace Expansion
# Quickly move between parallel directory structures
cd /var/log/{nginx,apache2,mysql}
# Copy files between similar paths
cp /etc/nginx/sites-{available,enabled}/mysite.conf
Shell-Specific Features
Bash: shopt Options
# Auto-correct minor typos in cd
shopt -s cdspell
cd /ect/nignx # Corrects to /etc/nginx
# Allow cd to use variable names
shopt -s cdable_vars
projects=~/projects
cd projects # Changes to ~/projects
Zsh: Advanced Features
# Zsh can cd without typing cd
setopt AUTO_CD
/etc/nginx # Just type the path
# Approximate matching
setopt CORRECT
cd /etc/ngix # Did you mean /etc/nginx? [y/n]
Pro Tips
- Use tab completion aggressively — type the minimum unique prefix and press Tab
- Combine with find:
cd $(find / -type d -name "nginx" 2>/dev/null | head -1) - Create aliases for frequent directories in your shell config
- Use
fzffor fuzzy directory search:cd $(find . -type d | fzf)
Directory navigation may seem trivial, but when you're managing dozens of servers and switching between configuration directories hundreds of times a day, these seconds add up to hours saved.