Navigation¶
Moving around the filesystem efficiently is fundamental to command-line productivity.
The Filesystem Hierarchy¶
Unix filesystems are organized as a tree starting from root (/):
/
├── bin/ # Essential binaries
├── etc/ # System configuration
├── home/ # User home directories (Linux)
├── Users/ # User home directories (macOS)
├── tmp/ # Temporary files
├── usr/ # User programs and data
│ ├── bin/ # User binaries
│ ├── local/ # Locally installed software
│ └── share/ # Shared data
└── var/ # Variable data (logs, etc.)
Print Working Directory (pwd)¶
Always know where you are:
Change Directory (cd)¶
Move to a different directory:
cd /etc # Absolute path
cd projects # Relative path
cd # Go to home directory
cd ~ # Also home directory
cd - # Previous directory
cd .. # Parent directory
cd ../.. # Two levels up
Home Directory Shortcuts¶
Previous Directory¶
The - shortcut toggles between last two directories:
Absolute vs Relative Paths¶
Absolute paths start from root (/):
Relative paths start from current directory:
cd projects # Same as ./projects
cd ./projects # Explicit relative
cd ../sibling # Up one, then into sibling
Special Directory References¶
| Symbol | Meaning |
|---|---|
/ | Root directory |
~ | Home directory |
. | Current directory |
.. | Parent directory |
- | Previous directory (cd only) |
Directory Stack (pushd/popd)¶
When working across multiple directories, the directory stack helps you jump back:
pushd - Push and Change¶
The stack shows most recent first.
popd - Pop and Return¶
dirs - View Stack¶
With line numbers:
Jump to Stack Position¶
Clear the Stack¶
Tab Completion¶
Bash can complete paths when you press Tab:
Double-tab shows options:
Wildcards in Paths¶
Navigate with glob patterns:
CDPATH Variable¶
Define directories to search when using cd:
Now from anywhere:
CDPATH Caveat
CDPATH can cause confusion in scripts. Consider using it only interactively.
Practical Navigation Patterns¶
Quick Project Access¶
Add to your .bashrc:
# Quick project navigation
proj() {
cd ~/projects/"$1" 2>/dev/null || echo "Project not found: $1"
}
Usage:
Bookmark Directories¶
# Save current directory
alias mark='pwd > ~/.marked_dir'
# Return to marked directory
alias back='cd "$(cat ~/.marked_dir)"'
Create and Enter Directory¶
Usage:
Navigation with Modern Tools¶
autojump / z / zoxide¶
These tools learn your habits and let you jump to frequent directories:
Install zoxide (recommended):
Add to .bashrc:
fzf for Directory Navigation¶
Fuzzy find directories:
Or with a function:
Common Mistakes¶
Spaces in Paths¶
Quote paths with spaces:
cd "My Documents" # Correct
cd My\ Documents # Also correct (escaped space)
cd My Documents # Wrong - tries cd to "My"
Case Sensitivity¶
Linux is case-sensitive; macOS is case-insensitive by default:
Missing Directories¶
Check if directory exists before changing:
Try It¶
-
Practice basic navigation:
-
Use the directory stack:
-
Create the
mkcdfunction and test it:
Summary¶
| Command | Purpose |
|---|---|
pwd | Show current directory |
cd <path> | Change directory |
cd or cd ~ | Go home |
cd - | Previous directory |
cd .. | Parent directory |
pushd <path> | Push to stack and change |
popd | Pop stack and return |
dirs | Show directory stack |