Navigating the Filesystem
What you'll learn
- •Understand the Unix filesystem hierarchy
- •Navigate between directories with cd
- •Use absolute and relative paths
- •Master shortcuts like ~ and ..
The Filesystem Tree
Unix organizes everything in a tree structure starting from the root directory /. Everything - files, directories, devices, even running processes - lives somewhere in this tree.
/
├── home/ # User home directories
│ └── swarup/ # Your stuff lives here
├── etc/ # System configuration
├── var/ # Variable data (logs, databases)
├── usr/ # User programs and utilities
├── tmp/ # Temporary files
└── bin/ # Essential command binaries
Note
Where Am I? (pwd)
The pwd command (print working directory) tells you your current location:
pwd
You'll see something like /home/swarup or /Users/swarup on macOS.
Moving Around (cd)
The cd command (change directory) moves you around:
cd /var/log
pwd
You're now in the system logs directory.
Absolute vs Relative Paths
Absolute paths start from root (/):
cd /home/swarup/projects
Relative paths start from your current location:
cd projects/website
If you're in /home/swarup, this takes you to /home/swarup/projects/website.
Essential Shortcuts
These shortcuts will save you thousands of keystrokes:
| Shortcut | Meaning |
|---|---|
~ | Your home directory |
. | Current directory |
.. | Parent directory |
- | Previous directory |
Examples
cd ~
Go home, no matter where you are.
cd ..
Go up one level.
cd ../..
Go up two levels.
cd -
Toggle between current and previous directory. Super useful!
Tip
Listing Contents (ls)
We saw ls briefly. Let's explore it more:
ls
Basic listing.
ls -l
Long format with details.
ls -la
Long format including hidden files (those starting with .).
ls -lh
Long format with human-readable sizes (KB, MB, GB instead of bytes).
Understanding ls -l Output
drwxr-xr-x 5 swarup staff 160 Dec 14 10:30 projects
-rw-r--r-- 1 swarup staff 1234 Dec 14 09:15 notes.txt
| Part | Meaning |
|---|---|
d or - | Directory or file |
rwxr-xr-x | Permissions (we'll cover this later) |
5 or 1 | Number of links |
swarup | Owner |
staff | Group |
160 | Size in bytes |
Dec 14 10:30 | Last modified |
projects | Name |
Tab Completion
This is a game-changer. Start typing a path and press Tab:
cd ~/Doc<Tab>
The shell completes it to ~/Documents/. If there are multiple matches, press Tab twice to see options.
Warning
Practice Exercise
- Navigate to your home directory
- Create a mental map: run
ls -laand identify hidden files (hint: they start with.) - Navigate to
/var/logand back home usingcd - - Try tab-completing a long path
Key Takeaways
- The filesystem is a tree starting at
/ pwdshows where you are,cdmoves you around- Use
~,.., and-to navigate efficiently - Tab completion saves time and prevents typos
ls -lais your go-to for seeing what's in a directory
Next up: creating, copying, and moving files.