Master directory navigation, understand absolute vs relative paths, and learn how file systems are organized across different operating systems.
Think of your computer's file system as a massive library. Just as libraries organize books into sections, shelves, and categories, your operating system organizes files into a hierarchical tree of folders (directories). Knowing how to navigate this tree efficiently is fundamental to being productive on the command line.
In a graphical interface, you click through folders visually. On the command line, you navigate using text commands - but once you master these, you'll move through your file system faster than any mouse can click.
Whether you're managing a simple project with a few files or navigating a complex enterprise codebase with thousands of directories, the navigation principles remain the same. Understanding paths - both absolute and relative - is like learning to read a map. Once you know the landmarks and shortcuts, you'll never get lost.
This lesson will transform your command-line navigation from hesitant and uncertain to fast and confident. You'll learn to think in paths, use shortcuts instinctively, and move through your file system with the speed that makes professional developers so productive.
Every operating system organizes files in a tree structure, starting from a root and branching out into folders (directories).
Unix/Linux/macOS Structure:
1/ (root - the very top)
2├── home/ (user home directories)
3│ ├── developer/
4│ │ ├── projects/
5│ │ ├── documents/
6│ │ └── downloads/
7│ └── john/
8├── usr/ (user programs and files)
9│ ├── bin/ (user binaries)
10│ └── local/
11├── etc/ (system configuration)
12├── var/ (variable data: logs, temp files)
13└── tmp/ (temporary files)Windows Structure:
1C:\ (drive root)
2├── Users\
3│ ├── Developer\
4│ │ ├── Documents\
5│ │ ├── Desktop\
6│ │ └── Downloads\
7│ └── John\
8├── Program Files\ (installed applications)
9├── Windows\ (operating system)
10└── temp\ (temporary files)Key Differences:
Path Separators:
/\ (but Git Bash accepts /)Root:
/C:\, D:\, etc.Home Directory:
/home/username/Users/usernameC:\Users\usernameImportant System Directories:
/home or /Users (Unix/Mac)
Your personal files live here. Each user has their own subdirectory.
/usr
User programs and shared resources (not "user" as in personal files).
/etc
System configuration files. Need admin rights to modify.
/var
Variable data: logs, caches, temporary application data.
/tmp
Temporary files, often cleared on restart.
Why This Matters:
Understanding the hierarchy helps you:
Paths are addresses that tell the system where to find a file or directory. Understanding the difference between absolute and relative paths is crucial.
Absolute Paths
Start from the root and specify the complete path to a file or directory.
Unix/Linux/macOS examples:
1/home/developer/projects/website/index.html
2/usr/local/bin/node
3/etc/nginx/nginx.confWindows examples:
1C:\Users\Developer\projects\website\index.html
2C:\Program Files\nodejs\node.exeCharacteristics:
/ (Unix) or drive letter (Windows)Relative Paths
Start from your current directory and specify the path relative to where you are.
Examples when in /home/developer:
1projects/website/index.html # Go into projects, then website
2./documents/notes.txt # ./ means "current directory"
3../john/files/data.json # .. means "up one level"Characteristics:
/ or drive letterWhen to Use Each:
Use Absolute Paths:
1# When writing system scripts
2cd /var/log
3tail -f /etc/nginx/error.log
4
5# When you want guaranteed behavior
6backup /home/developer/projects /backup/location
7
8# In configuration files
9DATABASE_PATH=/var/lib/postgres/dataUse Relative Paths:
1# When navigating nearby
2cd ../projects
3cd ./subdirectory
4
5# In project structures
6import './components/Header'
7<img src="../images/logo.png">
8
9# When path might change
10cd ~/projects # ~ expands to your home, works for all usersPath Components:
. (single dot) - Current directory
1./script.sh # Run script in current directory
2ls ./ # List current directory (same as just "ls").. (double dot) - Parent directory
1cd .. # Go up one level
2cd ../.. # Go up two levels
3cd ../../projects # Go up two levels, then into projectsPractical Example:
1# Current location: /home/developer/projects/website/src
2
3pwd
4# Output: /home/developer/projects/website/src
5
6# Absolute path to reach /home/developer/documents:
7cd /home/developer/documents
8
9# Relative path to reach the same location:
10cd ../../documents
11# (up to website, up to projects, then down to documents)
12
13# Which is better? Relative! It works even if:
14# - Different username (not just "developer")
15# - Different home directory location
16# - Project moved to different locationUnderstanding both types and when to use each makes you a more effective command-line user.
Master these core commands to move confidently through your file system.
pwd - Print Working Directory
Shows your current location (where you are right now).
1pwd
2# Output: /home/developer/projectsAlways use this when:
ls - List Directory Contents
Shows files and folders in a directory.
1# Basic list
2ls
3
4# Long format (detailed information)
5ls -l
6
7# Show all files (including hidden ones starting with .)
8ls -a
9
10# Combine flags: long format + all files + human-readable sizes
11ls -lah
12
13# List specific directory
14ls /home/developer
15
16# List with colors and indicators (/ for directories, * for executables)
17ls -F --color=autoUnderstanding ls -l output:
1-rw-r--r-- 1 developer staff 2048 Jan 15 10:30 README.md
2│ │ │ │ │ │ └─ filename
3│ │ │ │ │ └─ modification date/time
4│ │ │ │ └─ size in bytes
5│ │ │ └─ group
6│ │ └─ owner
7│ └─ number of links
8└─ permissions (covered in later lesson)cd - Change Directory
Move to a different directory.
1# Go to specific directory
2cd /home/developer/projects
3
4# Go to home directory (all three do the same thing)
5cd
6cd ~
7cd $HOME
8
9# Go up one level
10cd ..
11
12# Go up two levels
13cd ../..
14
15# Go to previous directory (like "back" button)
16cd -
17
18# Go into subdirectory
19cd projects/website/src
20
21# Combine with ~ for home-relative paths
22cd ~/projects
23cd ~/documentsPro Tips:
Tab Completion
Type first few letters, press Tab:
1cd proj<Tab> # Completes to "cd projects"
2cd ~/doc<Tab> # Completes to "cd ~/documents"If multiple matches exist, press Tab twice to see options:
1cd ~/d<Tab><Tab>
2# Shows: documents/ downloads/ desktop/Fast Navigation Pattern
1# Jump to project
2cd ~/projects/my-app
3
4# Quick exploration
5ls
6cd src
7ls
8cd components
9ls
10
11# Back to project root quickly
12cd ~/projects/my-app
13# or
14cd - # if it was your previous locationPowerShell Equivalents:
1Get-Location # Same as pwd
2Set-Location # Same as cd (but 'cd' works too)
3Get-ChildItem # Same as ls (but 'ls' and 'dir' work too)Common Mistakes to Avoid:
❌ cd folder name (spaces need quotes or escaping)
✅ cd "folder name" or cd folder\ name
❌ cd /projects (starts from root, probably not what you want)
✅ cd projects or cd ~/projects
❌ ls -la > output.txt then forget where the file went
✅ pwd first, or use absolute path: ls -la > ~/output.txt
Practice these commands until you can navigate without thinking. Speed comes from muscle memory.
Learn the shortcuts that professional developers use to navigate lightning-fast.
Tilde (~) - Home Directory
The ~ character expands to your home directory.
1cd ~ # Go home
2cd ~/projects # Go to projects in your home
3ls ~/documents # List documents in your home
4mkdir ~/new-folder # Create folder in your home
5
6# Works in any command
7cat ~/notes.txt
8cp ~/file.txt ~/backup/Why use ~ instead of full path?
1# Portable - works for any user
2cd ~/projects # Works whether you're "developer", "john", or "admin"
3
4# vs. Hardcoded - breaks if username changes
5cd /home/developer/projects # Only works for user "developer"Dot (.) - Current Directory
Refers to your current location.
1# Run script in current directory
2./run-tests.sh
3
4# Copy to current directory
5cp ~/downloads/file.txt .
6
7# Show hidden files in current directory
8ls -la .
9
10# Current directory in path
11chmod +x ./script.shDouble Dot (..) - Parent Directory
Go up one level in the directory tree.
1# Go up one level
2cd ..
3
4# Go up two levels
5cd ../..
6
7# Navigate relatively
8cd ../sibling-folder
9cd ../../grandparent/aunt/cousin
10
11# Reference files in parent
12cat ../README.md
13cp ../config.json .Dash (-) - Previous Directory
Jump back to where you were before.
1# Navigate to project
2cd ~/projects/website
3
4# Jump to logs directory
5cd /var/log
6
7# Jump back to website
8cd -
9
10# Toggle between two directories
11cd ~/projects
12cd ~/documents
13cd - # Back to projects
14cd - # Back to documents
15cd - # Back to projects
16# ... you get the ideaSlash (/) - Root Directory
The very top of the file system.
1# Go to root
2cd /
3
4# List root contents
5ls /
6
7# Absolute paths start with /
8cd /home/developer
9cd /usr/local/bin
10cd /etc/nginxCombining Shortcuts:
1# From anywhere, go to project directory
2cd ~/projects/my-app/src
3
4# View file in parent directory's sibling
5cat ../../docs/README.md
6
7# Copy from home to current directory
8cp ~/downloads/data.json .
9
10# Create in parent directory
11mkdir ../new-folder
12
13# Complex navigation
14cd ~/projects/old-project/../new-project/src
15# Same as: cd ~/projects/new-project/srcEnvironment Variables for Paths:
$HOME - Your home directory (same as ~)
1echo $HOME
2cd $HOME/projects$PWD - Current directory (print working directory)
1echo $PWD
2cd $PWD/subdirectory$OLDPWD - Previous directory (where cd - goes)
1echo $OLDPWD
2cd $OLDPWDReal-World Examples:
Scenario 1: Quick Project Setup
1cd ~/projects
2mkdir new-app && cd new-app
3mkdir src docs tests
4cd src
5touch index.js app.js
6cd ..
7pwd # See where you ended upScenario 2: Navigate Complex Structure
1cd ~/projects/website/backend/src/controllers
2# Oops, need to check something in frontend
3cd ../../frontend/src/components
4# Back to backend
5cd ../../backend/src/controllers
6# Or use the - shortcut if you toggle often
7cd -Scenario 3: Copy Files Around
1cd ~/projects/app/src
2cp ~/downloads/config.json . # To current directory
3cp ../README.md ~/documents/backup/ # From parent to somewhere else
4cp ./utils/*.js ../legacy/utils/ # Using wildcardsThese shortcuts become second nature with practice. Professional developers rarely type full paths - they think in shortcuts.
Wildcards let you operate on multiple files at once using patterns instead of typing each filename.
**Asterisk (*) - Matches Any Characters**
The most commonly used wildcard.
1# List all JavaScript files
2ls *.js
3
4# List all files starting with "test"
5ls test*
6
7# List all files ending with "config"
8ls *config*
9
10# Delete all log files
11rm *.log
12
13# Copy all text files to backup
14cp *.txt ~/backup/
15
16# Find all JSON files in all subdirectories
17find . -name "*.json"Question Mark (?) - Matches Single Character
Use when you know the exact length of the filename.
1# List files like: file1.txt, file2.txt, fileA.txt
2ls file?.txt
3
4# List: test1.js, test2.js (but not test10.js)
5ls test?.js
6
7# Three-letter files with .log extension
8ls ???.logSquare Brackets ([]) - Matches Character Sets
Specify a range or set of characters.
1# Match file1.js, file2.js, file3.js (not fileA.js)
2ls file[123].js
3
4# Match any digit
5ls file[0-9].js
6
7# Match any letter
8ls file[a-z].js
9
10# Match specific characters
11ls file[abc].js # Matches: filea.js, fileb.js, filec.js
12
13# Negate with !
14ls file[!0-9].js # Matches: fileA.js, not file1.jsCurly Braces ({}) - Matches Alternatives
Specify multiple complete options.
1# Match multiple extensions
2ls *.{js,ts,jsx,tsx}
3
4# Multiple filenames
5cat {README,LICENSE,CHANGELOG}.md
6
7# Create multiple directories
8mkdir {src,dist,tests,docs}
9
10# Backup with date
11cp config.json config.json.{backup,$(date +%Y%m%d)}Practical Use Cases:
1. Clean Up Temporary Files
1# Delete all temp files
2rm *.tmp
3
4# Delete all backup files
5rm *~
6rm *.bak
7
8# Delete all log files older than pattern
9rm *.log.*2. Work with Specific File Types
1# Count all JavaScript files
2ls -l *.js | wc -l
3
4# Find all image files
5ls *.{jpg,jpeg,png,gif,webp}
6
7# Copy all CSS and SCSS files
8cp *.{css,scss} ~/styles-backup/3. Process Multiple Related Files
1# List all test files
2ls *test.js
3ls *.test.js
4ls test*.js
5
6# View all configuration files
7cat *config.json
8cat .{eslintrc,prettierrc,babelrc}4. Batch Renaming (with loop)
1# Rename all .txt files to .md
2for file in *.txt; do
3 mv "$file" "${file%.txt}.md"
4done
5
6# Add prefix to all images
7for img in *.jpg; do
8 mv "$img" "photo-$img"
9doneAdvanced Patterns:
1# All JavaScript files in any subdirectory
2ls */*.js
3
4# All files two levels deep
5ls */*/*.txt
6
7# All files starting with "test" in subdirectories
8ls */test*
9
10# Combine patterns
11ls src/**/*.{js,jsx} # All JS/JSX files in src tree (Bash 4+)Safety Tips:
⚠️ Always test before destructive operations:
1# BAD: Delete all logs immediately
2rm *.log
3
4# GOOD: List first, verify, then delete
5ls *.log
6# Check the output looks correct
7rm *.log⚠️ Be careful with spaces:
1# BAD: Will fail with spaces in filenames
2rm *.txt
3
4# GOOD: Use quotes in loops
5for file in *.txt; do
6 rm "$file"
7done⚠️ Watch out for unexpected matches:
1# Might match more than you think
2rm test* # Matches: test1, test2, testing, test-data, test.txt...
3
4# Be more specific
5rm test[0-9]* # Only matches: test1, test2, test123...PowerShell Wildcards:
1# Same wildcards work in PowerShell
2Get-ChildItem *.js
3Remove-Item *.log
4Copy-Item *.txt backup\Wildcards are powerful tools that separate beginners from efficient CLI users. Practice with ls first before using with destructive commands.
Professional tips and techniques for lightning-fast navigation.
pushd and popd - Directory Stack
Navigate multiple directories and return to previous locations.
1# Save current directory and move to new one
2pushd ~/projects/website
3# Do work...
4pushd ~/documents
5# Do more work...
6
7# Return to previous directory
8popd # Back to ~/projects/website
9popd # Back to original location
10
11# View directory stack
12dirsCDPATH - Custom Navigation Paths
Set directories that cd will search.
1# Add to ~/.bashrc or ~/.zshrc
2export CDPATH=".:~:~/projects:~/documents"
3
4# Now you can cd to any project directly
5cd my-website # Finds ~/projects/my-website automatically
6cd work-docs # Finds ~/documents/work-docs automaticallyAliases for Common Paths
Create shortcuts for frequent destinations.
1# Add to ~/.bashrc or ~/.zshrc
2alias proj="cd ~/projects"
3alias docs="cd ~/documents"
4alias web="cd ~/projects/website/src"
5alias api="cd ~/projects/api-server"
6
7# Now just type:
8proj # Instantly jump to ~/projects
9web # Jump to website sourceDirectory Bookmarks (with shell plugins)
Using z (jump around):
1# After visiting directories a few times, z learns
2z website # Jumps to most frequent "website" directory
3z api # Jumps to most frequent "api" directory
4z src # Jumps to most frequent "src" directorySmart Tab Completion
Modern shells have intelligent completion.
1# Type partial path, press Tab
2cd ~/pro<Tab> # Completes to ~/projects/
3cd ~/projects/we<Tab> # Completes to ~/projects/website/
4
5# Press Tab twice to see all options
6cd ~/projects/<Tab><Tab>
7# Shows all subdirectories in projectsCommand Substitution in Paths
Use command output as part of paths.
1# Change to the directory of a file
2cd $(dirname $(which node))
3
4# Go to directory from file content
5cd $(cat .project-path)
6
7# Use find results
8cd $(find . -name "package.json" -exec dirname {} \; | head -1)Efficient Multi-Directory Workflows
Pattern 1: Temporary Exploration
1# Save location, explore, return
2pushd ~/projects
3cd src/components
4ls
5cd ../utils
6ls
7popd # Back where you startedPattern 2: Side-by-Side Work
1# Terminal 1: Frontend
2cd ~/projects/website/frontend
3npm run dev
4
5# Terminal 2: Backend
6cd ~/projects/website/backend
7npm start
8
9# Terminal 3: Database
10cd ~/projects/website
11docker-compose up postgresPattern 3: Quick Fixes
1# Fix file in another location
2pwd # Remember where you are
3cd /etc/nginx
4sudo vim nginx.conf
5cd - # Return to previous location immediatelyPro Shortcuts Combination:
1# Create and enter directory in one command
2mkdir -p ~/projects/new-app/{src,tests,docs} && cd ~/projects/new-app
3
4# Jump home, grab file, return
5cd ~ && cp config.json $OLDPWD && cd -
6
7# Quick parent navigation
8cd ../../.. # Go up three levels
9alias ...="cd ../.."
10alias ....="cd ../../.."Visual Directory Tree
1# Install tree command, then:
2tree # Show directory tree
3tree -L 2 # Limit to 2 levels deep
4tree -d # Only show directories
5tree -I "node_modules" # Ignore node_modulesFinding Files and Navigating to Them
1# Find file and cd to its directory
2cd $(find ~/projects -name "package.json" | head -1 | xargs dirname)
3
4# Or with fd (modern find alternative)
5cd $(fd package.json ~/projects -1 | xargs dirname)Keyboard Shortcuts Recap:
Ctrl+A - Beginning of lineCtrl+E - End of lineCtrl+U - Clear line before cursorCtrl+K - Clear line after cursorCtrl+W - Delete word before cursorCtrl+L - Clear screen (same as clear)Alt+B - Move back one wordAlt+F - Move forward one wordThe Professional's Mental Model:
Think in paths, not clicks:
~~/projects...-Chain your navigation:
1cd ~/projects && ls -la
2cd ../docs || echo "docs doesn't exist"
3cd config && vim settings.json && cd -Use the right tool:
cdaliaspushd/popdz pluginPractice these techniques until navigation becomes unconscious. Speed and confidence come from muscle memory and mental models, not memorizing commands.
Your progress is saved in this browser, on this device only.