When working with Linux, managing disk space is crucial. Two commonly used commands for checking disk usage are du (disk usage) and df (disk free). While they seem similar, they serve different purposes. Let's explore their differences with examples and real-world use cases.
1. What is du?
The du (disk usage) command shows how much space files and directories are consuming on the disk. It helps find large files and directories.
sudo du -sh /*
Breaking Down the Command
- du (Disk Usage) → Reports the amount of space used by files and directories.
- -s (Summarize) → Displays only the total size of each directory, instead of listing sizes of all subdirectories and files.
- -h (Human-readable) → Converts sizes into readable formats like KB, MB, or GB instead of raw bytes.
- /* (All Top-Level Directories) → It calculates the size of each top-level directory under /.
Output
4.0K /bin
16K /boot
0 /dev
12M /etc
120G /home
4.0G /lib
0 /proc
50G /usr
20G /var
Explanation:
- Each line represents the disk usage of a top-level directory.
- The size is in human-readable format (-h flag).
- /home, /usr, and /var typically consume the most space.
- /dev and /proc show 0 because they contain virtual files that don’t take up disk space.
More Example:
du -sh /home/user/Documents
Output:
2.5G /home/user/Documents
This means the /home/user/Documents directory is using 2.5 GB of disk space.Basically we can find the disk usage of a specific folder also by giving its path.
Useful Options available with du :
- -h (human-readable): Shows sizes in KB, MB, GB.
- -s (summary): Shows only the total size of a directory.
- -a (all files): Displays sizes of both files and directories.
Use Cases of du:
Find the largest directories on your system:
du -ah / | sort -rh | head -10
This command sorts directories by size and lists the top 10 largest ones.
Analyze space usage of a specific user:
du -sh /home/username
This helps system administrators track user disk usage.
Monitor growing log files:
du -sh /var/log
Helps find large log files that may need rotation or deletion.
2. What is df?
The df (disk free) command shows the available and used space on file systems, helping monitor overall disk usage.
Example-1 : when we run on any linux machine
df -h
output
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 100G 75G 25G 75% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 1.1M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda2 200G 150G 50G 75% /home
/dev/sdb1 50G 10G 40G 20% /mnt/external_drive
tmpfs 796M 16K 796M 1% /run/user/1000
Example-2 : when we run on aws ec2 linux machine
df -h
output
Filesystem Size Used Avail Use% Mounted on
/dev/root 8.0G 3.2G 4.8G 40% /
devtmpfs 481M 0 481M 0% /dev
tmpfs 495M 0 495M 0% /dev/shm
tmpfs 495M 436K 495M 1% /run
tmpfs 495M 0 495M 0% /sys/fs/cgroup
/dev/loop0 69M 69M 0 100% /snap/core18/2785
/dev/loop1 45M 45M 0 100% /snap/snapd/19457
tmpfs 99M 0 99M 0% /run/user/1000
Difference :
When you run df -h on an AWS EC2 instance, you often see an entry like /dev/root instead of /dev/sda1. This happens because AWS uses virtualized storage, and the root filesystem is typically mounted from an Elastic Block Store (EBS) volume.
Useful Options available with df:
- -h (human-readable): Displays sizes in KB, MB, GB.
- -T (filesystem type): Shows filesystem types like ext4, xfs, etc.
- -i (inodes): Displays inode usage instead of disk usage.
Use Cases of df:
Check available space on all mounted filesystems:
df -hT
This helps in monitoring different storage partitions.
Find which partition is nearly full:
df -h | grep -E 'Filesystem|%'
Useful for proactively cleaning up space before a system runs out of storage.
Check inode usage (for small file-heavy servers):
df -i
If inodes are exhausted, no new files can be created even if space is available.
3. Key Differences Between du and df
4. Why Do du and df Show Different Values?
Sometimes, du and df may show different results because:
- Deleted but open files: If a process is using a deleted file, df still counts its space.
- Different mount points: df reports disk-wide usage, while du only shows a selected directory.
- Files stored in different places: Disk compression, symlinks, and snapshots can cause variations.
Example of Difference:
echo "Hello, world!" > testfile.txt
rm testfile.txt
du -sh .
df -h .
Even though the file is deleted, df might still show the space as used until the process holding it is closed.
5. When to Use du and df?
- Use du to find large files and analyze specific directory usage.
- Use df to check overall disk space and monitor filesystem health.
Conclusion
Understanding du and df helps efficiently manage disk space. Use du for detailed file usage and df for a high-level disk overview. Next time you see "No space left on device," use these commands to diagnose the issue!
I hope this blog makes it easier to understand du and df. Let me know if you want more examples or explanations!
Top comments (0)