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

1. What You Will Learn


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":

BASH
# 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:

💡 Tip: The core principle of FHS (Filesystem Hierarchy Standard) is "partition by function" — /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: /tmp is typically cleared after a system reboot (modern systems use tmpfs, storing /tmp in 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 like ls, 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)
TEXT
# 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)
💡 Tip: 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.

TEXT
# 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

TEXT
# 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
BASH
# 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

BASH
# 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

TEXT
# 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

TEXT
# 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.

BASH
# 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:

TEXT
total 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

TEXT
# 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

BASH
# 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 in ls output.

Q: What does the first column drwxr-xr-x in ls -l output mean? A: The first character is the file type (d for directory, - for regular file, l for 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 /tmp disappear after a reboot? A: By default, yes. Many systems clean /tmp on startup. Therefore, /tmp is 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/popd to manage a directory stack, or the CDPATH environment variable.


📖 Summary


📝 Exercises

  1. Basic (Difficulty ⭐): Use pwd to confirm your current location, then cd / to go to the root directory, and ls -la / to list all files
  2. Intermediate (Difficulty ⭐⭐): Use cd ~ to go home and view hidden files, then go to /var/log to see the log directory, use cd - to toggle between the two, and finally use ls *.conf in /etc to find configuration files
  3. 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
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏