π½ Linux/Unix: Disk Usage Commands β df, du Explained with Examples
Introduction β Why Learn Disk Usage Commands in Linux?
Running out of disk space can lead to failed installs, corrupted logs, and halted services. Knowing how to monitor disk usage is essential for maintaining a healthy Linux/Unix system. Commands like df and du help you quickly identify disk space usage by filesystem or directory, so you can clean up and plan effectively.
In this guide, youβll learn:
- How to check free and used disk space with
df - How to find large files and folders with
du - Real-world examples and practical flags for scripting or analysis
1. df β Display Filesystem Disk Space Usage
What is df?
The df (disk free) command shows used and available space on mounted filesystems.
Syntax:
df [options] [path]
Common Options:
| Option | Description |
|---|---|
-h | Human-readable (e.g., MB, GB) |
-T | Show filesystem type |
-x | Exclude filesystem type |
--total | Show grand total summary |
Example 1: Show space on all mounted filesystems
df -h
Output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 32G 15G 69% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
Useful for checking overall system usage.
Example 2: Check specific mount point
df -h /var
Example 3: Include filesystem type
df -hT
Output:
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 ext4 50G 32G 15G 69% /
2. du β Estimate File & Directory Space Usage
What is du?
The du (disk usage) command shows the space used by directories or files, useful for finding large folders.
Syntax:
du [options] [path]
Common Options:
| Option | Description |
|---|---|
-h | Human-readable sizes |
-s | Summarize total only |
-a | Show all files and subdirs |
--max-depth=N | Limit recursion depth |
--exclude=PATTERN | Ignore certain paths |
Example 1: Check size of a folder
du -sh /home/user
Output:
2.1G /home/user
Example 2: Show size of all subfolders in /var
du -h --max-depth=1 /var
Output:
200M /var/log
3.5G /var/lib
120K /var/spool
Quickly locate heavy directories.
Example 3: Find top 10 largest folders
du -ah / | sort -rh | head -n 10
Output:
4.1G /var/lib/mysql
2.0G /home/user/Videos
1.5G /opt/docker/images
Tool Comparison: df vs du
| Feature | df | du |
|---|---|---|
| Shows usage of | Filesystems (mount points) | Individual directories/files |
| Ideal for | Checking total disk space | Finding space hogs |
| Default unit | KB (use -h for human-readable) | KB (use -h) |
| Recursion |
Summary β Recap & Next Steps
The df and du commands are essential tools for disk space auditing, cleanup planning, and managing storage resources on Linux systems. Use them regularly to monitor space usage, prevent full disks, and optimize performance.
Key Takeaways:
- Use
df -hto see how much space is left on mounted filesystems. - Use
du -shto check how large a folder is. - Combine
duwithsortto find large directories consuming space.
FAQs
Why does du and df show different values?
df checks free space on the filesystem level; du scans actual file sizes. Differences may include reserved space or open-but-deleted files.
How to find large files using du?
Try:
du -ah / | sort -rh | head -n 10
How do I check disk usage on a mounted USB drive?
Use:
df -h /mnt/usb
Can I exclude certain folders from du?
Yes:
du -h --exclude=/home/user/.cache /home/user
Is there a GUI alternative to du?
Yes, tools like baobab or ncdu (CLI visual tool) offer easier views.
Share Now :
