Navigating the Filesystem
Navigating the Filesystem#
Concepts#
Everything Is a File#
In Linux, (almost) everything is represented as a file: regular files, directories, hardware devices, running processes, and even network sockets. This is a fundamental design principle of Unix/Linux systems.
A directory is just a special file that contains a list of other files and directories.
The Directory Tree#
Linux has a single directory tree starting from the root directory /. There are no drive letters like C:\ or D:\ on Windows. Everything lives under /:
/
├── home/
│ └── kmiguel/
│ ├── Documents/
│ ├── Downloads/
│ └── .bashrc
├── etc/
├── var/
├── usr/
└── tmp/
Even if you plug in a USB drive, it appears as a directory under /media/ or /mnt/ — not as a separate drive letter.
Absolute vs Relative Paths#
-
Absolute path — Starts from the root
/. Always begins with/. Unambiguous./home/kmiguel/Documents/etc/hosts
-
Relative path — Starts from your current directory. Does not begin with
/.Documents(means./Documents— inside the current directory)../Downloads(one directory up, then into Downloads)
Special Directory Shortcuts#
| Symbol | Meaning | Example |
|---|---|---|
/ |
Root directory | cd / |
~ |
Home directory (/home/yourusername) |
cd ~ or just cd |
. |
Current directory | ./script.sh |
.. |
Parent directory (one level up) | cd .. |
- |
Previous directory (where you were before) | cd - |
Core Navigation Commands#
pwd — Print Working Directory#
Shows your current location in the directory tree:
pwd
# /home/kmiguel
ls — List Directory Contents#
ls # list files in current directory
ls /etc # list files in /etc
ls -l # long format (permissions, owner, size, date)
ls -a # show all files including hidden (starting with .)
ls -lh # long format with human-readable sizes (K, M, G)
ls -lt # long format, sorted by modification time (newest first)
ls -lS # long format, sorted by size (largest first)
ls -R # recursive (list subdirectories too)
ls -d */ # list only directories
The long format (ls -l) output looks like:
-rw-r--r-- 1 kmiguel kmiguel 3771 Oct 15 12:00 .bashrc
drwxr-xr-x 2 kmiguel kmiguel 4096 Oct 15 12:00 Documents
You will learn to read every column of this output in Module 02 (File Permissions). For now, note:
- Lines starting with
dare directories - Lines starting with
-are regular files
cd — Change Directory#
cd /etc # go to /etc (absolute path)
cd Documents # go into Documents (relative path)
cd .. # go up one level
cd ../.. # go up two levels
cd ~ # go home
cd # also goes home (shorthand)
cd - # go back to where you were before
Combining What You Know#
A typical workflow:
pwd # where am I?
ls # what's here?
cd Documents # go into Documents
ls -la # what's here, including hidden files?
cd .. # go back up
cd /var/log # jump to an absolute path
ls -lt # list by time, see what changed recently
cd - # go back to where I was (Documents parent)
The tree Command#
tree gives a visual representation of directory structure:
# Install it first (it may not be pre-installed)
sudo apt install tree
# Show the tree of the current directory
tree
# Limit depth
tree -L 2
# Show hidden files
tree -a
# Only directories
tree -d
Lab#
Exercise 1: Find Your Bearings#
# Where are you?
pwd
# What's in your home directory?
ls
# What's here including hidden files?
ls -la
# How many files and directories are in your home?
ls -la | wc -l
# (Don't worry about the | and wc — you'll learn pipes in Module 06)
Exercise 2: Move Around#
# Go to the root directory
cd /
# List what's at the root
ls
# Go to /etc
cd /etc
# Confirm
pwd
# List some files
ls
# Go back home using ~
cd ~
# Confirm you're home
pwd
# Go to /var/log
cd /var/log
# Go back to where you just were (home)
cd -
# Confirm
pwd
Exercise 3: Relative Paths#
# Start from home
cd ~
# Go into Documents using a relative path
cd Documents
# Confirm
pwd
# Should show: /home/yourusername/Documents
# Go up one level
cd ..
# Confirm you're in your home directory
pwd
# Go up two levels (to /home, then to /)
cd ../..
# Confirm
pwd
# Should show: /
# Go home in one command
cd
Exercise 4: Explore the System#
# Look at the top-level directories
ls /
# See what's in /etc (configuration files)
ls /etc
# See what's in /var/log (log files)
ls /var/log
# See what's in /tmp (temporary files)
ls /tmp
# See what's in /usr/bin (user programs)
ls /usr/bin | head -20
# (head -20 shows just the first 20 lines)
Exercise 5: Use tree#
# Install tree
sudo apt install -y tree
# View your home directory structure (limited to 2 levels)
tree -L 2 ~
# View only directories in /etc (limited to 1 level)
tree -d -L 1 /etc
# View your home with hidden files (limited to 1 level)
tree -a -L 1 ~
Exercise 6: Tab Completion Practice#
Navigate using Tab completion to build muscle memory:
# Type: cd /e then Tab → completes to cd /etc/
# Type: cd /etc/apt then Tab → completes to cd /etc/apt/
# Type: ls /v then Tab → /var/
# Type: ls /var/l then Tab → /var/log/
Try to navigate to /usr/share/doc/ using only Tab for completion. You should only need to type a few characters per segment.
Review#
1. What is the root directory and what is its path?
The root directory is the top of the entire filesystem tree. Its path is /. Everything on the system lives somewhere under /.
2. What is the difference between an absolute path and a relative path?
An absolute path starts from the root / (e.g., /home/kmiguel/Documents). A relative path starts from the current directory (e.g., Documents or ../Downloads). Absolute paths are unambiguous; relative paths depend on where you are.
3. What does `~` mean in a path?
It is shorthand for your home directory (/home/yourusername). For example, ~/Documents expands to /home/yourusername/Documents.
4. What does `cd -` do?
It takes you back to the previous directory — the directory you were in before your last cd command. It is like an “undo” for navigation.
5. How do you list hidden files?
Use ls -a (or ls -la for the long format). Hidden files in Linux are files whose names start with a dot (.), such as .bashrc or .config/.
6. What does `cd ..` do? What about `cd ../..`?
cd .. moves up one directory (to the parent). cd ../.. moves up two directories. .. is a special reference to the parent directory.
7. How does Linux handle external drives (USB, etc.) compared to Windows?
Linux does not use drive letters. External drives are “mounted” as directories under the filesystem tree, typically under /media/ (auto-mounted removable media) or /mnt/ (manually mounted drives). They appear as regular directories.
Previous: The Terminal | Next: File Operations