cd Command Guide
The cd (change directory) command is fundamental for navigating the Linux filesystem. Learn how to move between directories efficiently.
4 min read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
cd /path/to/dirChange to directorycdGo to home directorypwdShow current pathShortcuts
cd ~Home directorycd -Previous directorycd ..Parent directoryPaths
cd /var/logAbsolute pathcd ./subdirRelative pathcd ../siblingSibling directoryTips
cd "My Dir"Path with spacespushd /pathPush to dir stackpopdPop from dir stackDownloadable Image Preview
Failed to generate preview
Basic Usage
The cd command changes the current working directory. Simply provide the path to the directory you want to navigate to.
bash
cd /path/to/directoryCommon Shortcuts
Linux provides several shortcuts for common navigation patterns.
Home directory (~)
bash
# Go to home directory
cd ~
# Or simply
cdPrevious directory (-)
bash
# Go back to the previous directory
cd -This is useful for toggling between two directories.
Parent directory (..)
bash
# Go up one directory
cd ..
# Go up two directories
cd ../..Current directory (.)
bash
# Stay in current directory (rarely used alone)
cd .Navigation Shortcuts
| ~ | Home directory |
| - | Previous directory |
| .. | Parent directory |
| . | Current directory |
| / | Root directory |
Absolute vs Relative Paths
Absolute paths
Absolute paths start from the root directory (/).
bash
cd /home/user/documents
cd /var/log
cd /etc/nginxRelative paths
Relative paths start from the current directory.
bash
# If you're in /home/user
cd documents # Goes to /home/user/documents
cd ./documents # Same as above
cd ../otheruser # Goes to /home/otheruserTip
Use
pwd (print working directory) to see your current location at any time.Practical Examples
Navigate to a project directory
bash
cd ~/projects/my-appNavigate with tab completion
bash
# Type partial name and press Tab to complete
cd Doc<Tab> # Completes to DocumentsNavigate to a directory with spaces
bash
# Use quotes or escape the space
cd "My Documents"
cd My\ DocumentsUsing environment variables
bash
cd $HOME
cd $PROJECT_DIRWarning
If cd fails, it usually means the directory doesn't exist or you don't have permission to access it. Use
ls to verify the directory exists.Tips and Tricks
Use pushd and popd for directory stack
bash
# Push current directory and change to new one
pushd /var/log
# Pop back to previous directory
popdCreate and navigate in one command
bash
mkdir -p new-project && cd new-projectSummary
The cd command is essential for filesystem navigation. Key takeaways:
- Use
cd ~or justcdto go home - Use
cd -to toggle between directories - Use
cd ..to go up one level - Use Tab completion for faster navigation
- Quote paths with spaces or use backslash escaping