Whether you’re managing a server or troubleshooting your Linux system, monitoring disk space is crucial. Two essential terminal commands—df
and du
—help you diagnose storage issues. Here’s how to use them like a pro.
1. df
(Disk Free): The Big Picture
Purpose: Check overall disk usage for mounted filesystems (e.g., partitions, drives).
Syntax: df [options] [path]
Key Options & Examples:
-
Basic Usage:
df -h # "-h": Human-readable sizes (KB, MB, GB)
Output:
Filesystem Size Used Avail Use% Mounted on /dev/sda1 20G 11G 7.8G 58% / tmpfs 3.9G 0 3.9G 0% /tmp
-
Show Filesystem Types (
-T
):df -Th # Adds a "Type" column (ext4, tmpfs, etc.)
-
Focus on Specific Filesystems (e.g., ext4):
df -ht ext4
2. du
(Disk Usage): Deep Dive into Directories
Purpose: Measure directory-level usage (ideal for finding space hogs).
Syntax: du [options] [directory]
Key Options & Examples:
-
Summarize Directory Usage (
-s
):du -sh /home # "-s": Summary total, "-h": Human-readable
Output:
24G /home
-
List Subdirectory Sizes (Depth=1):
du -h --max-depth=1 /var/log
Output:
4.0K /var/log/apt 800M /var/log/journal 801M /var/log
-
Find Large Files (Combine with
sort
):du -h /var | sort -rh | head -n 5 # Top 5 space-consuming dirs
3. When to Use Which
-
Use
df
when:- You need a quick overview of mounted filesystems.
- Checking if a partition is full (e.g.,
/dev/sda1 at 95%
).
-
Use
du
when:- You need to identify which directories/files consume space.
- Auditing storage in
/home
,/var
, or other user-data locations.
4. Pro Tips
- Exclude Hidden Files (e.g.,
.cache
):du -sh --exclude=".*" /home/user
df
Caveat: It reports filesystem metadata, soUsed + Avail
may not equalSize
(reserved space exists).- Combine Both:
df -h /home # Check partition usage du -sh /home/* # Then scan directories
Conclusion
df
gives you a helicopter view of disk health, while du
drills into directory details. Together, they’re indispensable for:
- Preventing “out of space” crashes.
- Cleaning up bloated systems.
- Optimizing server storage.
Try these commands now! Run df -h
and du -sh ~/
to start exploring your own disk usage. 🚀
> Note: Always use -h
for readability, and prefix destructive commands (like rm
) with sudo
cautiously!