cd - Moving Around
cd stands for Change Directory. It moves you from one directory to another.
Basic Usage
After cd, you're in a new location. All relative commands now operate from there.
Going Home
Several ways to get to your home directory:
Just cd with no arguments takes you home. It's the fastest way.
Lost? Go Home
When you're lost in the file system, cd (no arguments) takes you home. Then you can start fresh.
Going Up
Use .. to move up one level:
Chain them to go up multiple levels:
Going to Previous Directory
The - shortcut takes you to your previous location:
This is fantastic when you're bouncing between two directories. It's like the "back" button.
Toggle Between Two
Working on a project and checking logs? Use cd - to toggle back and forth. Way faster than typing full paths.
Absolute vs Relative
# Absolute - start from root
cd /var/log
# Relative - start from current directory
cd logs/nginx
Both work. Use absolute when you know exactly where you're going. Use relative for nearby directories.
Common Patterns
Jump to a Deep Directory
Create and Enter
Quick Back-and-Forth
Advanced: pushd/popd (Directory Stack)
If you're bouncing between more than two directories, use pushd and popd:
pushd saves your current location to a stack before moving. popd pops you back. Use dirs to see the stack.
When to Use pushd
Great for temporarily jumping somewhere, doing work, then returning. Think: "I'll need to come back here." That's a pushd moment.
What cd Can't Do
cd only changes directories. It can't:
- Change to a file (
cd file.txt- error) - Create directories (use
mkdirfirst) - Work if the directory doesn't exist
Quick Reference
| Command | Where It Goes |
|---|---|
cd | Home directory |
cd ~ | Home directory |
cd .. | One level up |
cd ../.. | Two levels up |
cd - | Previous directory |
cd /path | Absolute path |
cd path | Relative path |
What does `cd -` do?
Key Takeaways
cdwith no arguments goes home..goes up one level-goes to previous directory (toggle mode)- Use absolute paths when you know the full location
- Use relative paths for nearby directories
cdonly works with directories, not files
Next: let's uncover the secret world of hidden files.