Linux: Filesystem Navigation
Linux organizes all files and directories into a tree rooted at /. This tree is called the Filesystem Hierarchy Standard (FHS).
📋 Prerequisites: You should first complete
- Lesson 3: Terminal Basics
1. What You Will Learn
- The three core navigation commands: pwd, cd, ls
- FHS standard directory structure
- Absolute paths vs relative paths
- Special meanings of
.and.. - Wildcards and hidden files
2. A Lost Beginner's Story
(1) The Pain: Not Knowing Where You Are in the Directory Tree
Alex logged into a server for the first time and was overwhelmed by the unfamiliar directory names under /: /bin, /etc, /var, /usr... He didn't know what each directory was for or where he currently was.
(2) Three Maps
Senior engineer Bob drew three "maps":
# Map 1: Where am I?
pwd
# Output: /home/charlie
# Map 2: What's here?
ls
# Output: Desktop Documents Downloads
# Map 3: Go there
cd /etc
(3) The Benefit: Free Navigation
Alex understood the relationship between the three commands: pwd locates your current position, ls shows your surroundings, and cd takes you to your destination. From then on, he could navigate freely through any Linux server's filesystem.
3. Knowledge Points
(1) FHS Standard Directory Quick Reference
Linux's directory structure follows a unified standard. You don't need to memorize every directory, but these are essential:
/etc for configuration, /var for variable data, /usr for user programs. Once you understand this logic, even if you forget a specific directory, you can infer where a file is likely located based on its function.
| Directory | Meaning | Purpose |
|---|---|---|
/ |
Root directory | The starting point of the filesystem; all paths begin here |
/home |
User home directories | Each user's personal files are stored here (e.g., /home/alice) |
/etc |
Configuration files | System config files and daemon configurations (e.g., Nginx, SSH) |
/var |
Variable data | Logs (/var/log), databases, cached data |
/tmp |
Temporary files | Temporary directory that is auto-cleaned on reboot |
ℹ️ Note:
/tmpis typically cleared after a system reboot (modern systems use tmpfs, storing/tmpin memory). Don't store important data or files you need long-term in/tmp— it's only suitable for temporary use. |/usr| User system resources | System programs and libraries (/usr/bin,/usr/lib)| |/bin| Essential commands | Basic commands likels,cp,mv| |/opt| Optional software | Third-party software installation directory |
(2) Absolute Paths vs Relative Paths
| Type | Feature | Example |
|---|---|---|
| Absolute path | Starts with /, from the root |
/home/alice/Documents/report.md |
| Relative path | Starts from the current directory | Documents/report.md (when in /home/alice) |
# Currently in /home/alice
# Using an absolute path
cd /etc/nginx
# Using a relative path (same effect, starting from current directory)
cd ../../../etc/nginx
(3) Special Meanings of . and ..
| Symbol | Meaning |
|---|---|
. |
Current directory |
.. |
Parent directory |
~ |
Current user's home directory |
~alice |
User alice's home directory |
- |
Previous directory (cd only) |
cd - lets you quickly switch back to your previous working directory — very handy when switching between two directories. For example, if you frequently toggle between /var/log and /etc/nginx, just type cd - each time.
# Currently in /home/alice/projects
cd . # No movement (stays in current directory)
cd .. # Move to /home/alice
cd ../.. # Move to /home
cd ~ # Move to /home/alice
cd - # Go back to previous directory
(4) Common ls Options
# Basic usage
ls # List filenames (compact)
ls -l # Long format (permissions/owner/size/time)
ls -a # Show all files (including hidden files starting with .)
ls -la # Common combination
ls -lh # Human-readable file sizes (KB/MB/GB)
ls -R # Recursively list subdirectories
ls -S # Sort by file size
ls -t # Sort by modification time
(5) Wildcards
| Wildcard | Match rule | Example |
|---|---|---|
* |
Any number of characters | *.txt matches all files ending in .txt |
? |
Single character | file?.md matches file1.md, file2.md |
[abc] |
Any one character in the set | file[12].md matches file1.md / file2.md |
[!abc] |
Exclude characters in the set | [!a-z]* matches files not starting with a letter |
# Wildcard examples
ls *.md # List all Markdown files
ls file?.txt # List file1.txt, file2.txt, etc.
ls [abc]* # List files starting with a, b, or c
ls [0-9]* # List files starting with a digit
(6) ▶ Example: pwd — Tell Me Where I Am
# Whenever you're unsure of your current location, use pwd
pwd
# Output: /home/alice
# Try after switching to a different directory
cd /etc
pwd
# Output: /etc
Output:
TEXT/home/alice /etc
(7) ▶ Example: cd — Take Me Through Directories
# Go home
cd ~
# Go back to previous directory
cd /etc
cd /var/log
cd - # Back to /etc
# Go to root
cd /
# Go to a specific user's home directory
cd ~alice
(8) ▶ Example: ls — Show Me What's Here
# Compact mode
ls /etc
# Long format (includes permissions, size, modification time)
ls -l /etc
# Show hidden files
ls -la ~
# Human-readable sizes
ls -lh /var/log
# Recursive view
ls -R ~/projects | head -20
# Sort by file size (largest first)
ls -lhS /var/log
(9) ▶ Example: Hidden Files
In Linux, any file or directory starting with . is hidden. ls doesn't display them by default.
# View hidden files in home directory
ls -la ~
# Common hidden files and directories
# .bashrc bash configuration
# .bash_profile Login Shell configuration
# .gitconfig Git configuration
# .ssh/ SSH keys and configuration
# .vimrc vim configuration
Output:
TEXTtotal 48 drwxr-xr-x 1 alice alice 4096 Jul 8 10:23 . drwxr-xr-x 1 alice alice 4096 Jul 8 10:23 .. -rw-r--r-- 1 alice alice 220 Jul 8 10:23 .bash_logout -rw-r--r-- 1 alice alice 3771 Jul 8 10:23 .bashrc -rw-r--r-- 1 alice alice 807 Jul 8 10:23 .profile drwx------ 2 alice alice 4096 Jul 8 10:23 .ssh -rw-r--r-- 1 alice alice 256 Jul 8 10:23 .vimrc
(10) ▶ Example: Wildcards in Action
# List all .log files
ls -la /var/log/*.log
# List files whose names start with "access"
ls -la /var/log/access*
# Delete all .tmp files
rm *.tmp
# Copy all .jpg files to another directory
cp *.jpg ~/Pictures/
(11) ▶ Comprehensive Example: Filesystem Adventure
# Alex's first filesystem adventure
# 1. Where am I?
pwd
# 2. See what's in the root directory
ls -la /
# 3. Go to /etc and check system configuration
cd /etc
ls | head -20
# 4. Go to /var/log for logs
cd /var/log
ls -lhS | head -10 # Top 10 largest log files
# 5. Go back home
cd ~
ls -la
# 6. See what's on the desktop
ls -la ~/Desktop
❓ FAQ
Q: What's the difference between
/and~? A:/is the root directory, the top level of the entire tree.~is the current user's home directory (usually/home/username). There's only one root, but every user has their own home directory.
Q: Why are files starting with
.hidden? A: It's a convention — files starting with.are considered "configuration files" or "system files" that users don't need to see in everyday use. This reduces noise inlsoutput.
Q: What does the first column
drwxr-xr-xinls -loutput mean? A: The first character is the file type (dfor directory,-for regular file,lfor link). The next 9 characters are split into three groups: file owner (user) permissions (3 bits), group permissions (3 bits), and others (other) permissions (3 bits). r=read, w=write, x=execute.
Q: Will files in
/tmpdisappear after a reboot? A: By default, yes. Many systems clean/tmpon startup. Therefore,/tmpis suitable for temporary files only — don't store important data there.
Q: How do I quickly switch between long paths? A: Use Tab completion (type the first few characters and press Tab),
cd -to go back,pushd/popdto manage a directory stack, or theCDPATHenvironment variable.
📖 Summary
pwdshows current location,cdswitches directories,lslists contents — three commands to navigate anywhere- FHS standard directories:
/homefor users,/etcfor configuration,/varfor logs,/tmpfor temporary files - Absolute paths start with
/; relative paths start from the current directory ..is parent directory,.is current directory,~is home directory*matches anything,?matches one character — powerful tools for batch operations
📝 Exercises
- Basic (Difficulty ⭐): Use
pwdto confirm your current location, thencd /to go to the root directory, andls -la /to list all files - Intermediate (Difficulty ⭐⭐): Use
cd ~to go home and view hidden files, then go to/var/logto see the log directory, usecd -to toggle between the two, and finally usels *.confin/etcto find configuration files - Advanced (Difficulty ⭐⭐⭐): Draw a directory structure diagram of your system's root directory (including at least 5 FHS standard directories), annotating each directory's purpose and whether it's writable