π½ 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 -h
to see how much space is left on mounted filesystems. - Use
du -sh
to check how large a folder is. - Combine
du
withsort
to 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 :