3. File System
3.1 Directory Structure Overview
Linux follows the everything is a file philosophy. All content (including hardware devices) exists as files. The entire file system starts from / (root directory), forming an inverted tree.
bash
# View root directory structure
ls /bash
bin boot dev etc home lib lib64 lost+found mnt opt proc root run sbin srv sys tmp usr var| Directory | Full Name | Purpose |
|---|---|---|
| / | root | Root directory, the starting point of all directories |
| /bin | binaries | Basic user commands (ls, cp, mv, etc.) |
| /sbin | system binaries | System administration commands (fdisk, iptables, etc.) |
| /etc | etcetera | System configuration files (equivalent to Windows Control Panel) |
| /home | home | Home directories for regular users |
| /root | root home | Home directory for the root user |
| /var | variable | Variable data (logs, caches, mail, etc.) |
| /tmp | temporary | Temporary files (may be cleared on reboot) |
| /usr | Unix System Resources | User-level programs and data (the largest directory) |
| /opt | optional | Third-party software installation location |
| /proc | process | Virtual filesystem for process and kernel information |
| /dev | device | Device files (hard drives, terminals, etc.) |
| /boot | boot | Boot loader files (kernel, grub) |
| /mnt | mount | Temporary mount points |
| /lib | library | System shared library files |
📝 Note: 📝 Memory trick: /etc can be understood as "Editable Text Configuration" — this is the easiest interpretation to remember.
3.2 Absolute Paths and Relative Paths
Absolute Path: a complete path starting from /, such as /home/user/Documents
Relative Path: a path relative to the current directory, not starting with /
Three special directory symbols:
.— current directory..— parent directory~— current user's home directory
bash
# Assuming we're currently in /home/user, the following two methods are equivalent
cd /home/user/Documents # Absolute path
cd Documents # Relative path
# Go to parent directory
cd ..
# Go up two directory levels
cd ../..
# Go to home directory (three equivalent methods)
cd ~
cd
cd $HOME3.3 File Types
In Linux, everything is a file. File types are identified by the first character of ls -l output:
bash
ls -l /bash
drwxr-xr-x 2 root root 4096 Dec 15 08:00 bin
drwxr-xr-x 3 root root 4096 Dec 15 08:00 boot
drwxr-xr-x 18 root root 3800 Jun 20 10:00 dev
drwxr-xr-x 96 root root 4096 Jun 20 10:00 etc
drwxr-xr-x 4 root root 4096 Jan 10 14:30 home
lrwxrwxrwx 1 root root 7 Dec 15 08:00 lib -> usr/lib
-rw-r--r-- 1 root root 64 Jun 20 09:00 test.txt| Identifier | Type | Description |
|---|---|---|
| - | Regular file | Text, binary, images, etc. |
| d | Directory | Folder |
| l | Symbolic link | Similar to Windows shortcuts |
| c | Character device | Terminals, keyboards, etc. |
| b | Block device | Hard drives, USB drives, etc. |
| p | Named pipe | Inter-process communication |
| s | Socket | Network communication |
Other Ways to View File Types:
bash
file /bin/ls
file /etc/passwd
file /dev/sdabash
/bin/ls: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, ...
/etc/passwd: ASCII text
/dev/sda: block special (8/0)