DEV Community

Sourav kumar
Sourav kumar

Posted on

Day-09/100 - Understanding the Difference Between du and df in Linux

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 /*
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Output:

2.5G    /home/user/Documents
Enter fullscreen mode Exit fullscreen mode

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 :

  1. -h (human-readable): Shows sizes in KB, MB, GB.
  2. -s (summary): Shows only the total size of a directory.
  3. -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
Enter fullscreen mode Exit fullscreen mode

This command sorts directories by size and lists the top 10 largest ones.

Analyze space usage of a specific user:

du -sh /home/username
Enter fullscreen mode Exit fullscreen mode

This helps system administrators track user disk usage.

Monitor growing log files:

du -sh /var/log
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Example-2 : when we run on aws ec2 linux machine

df -h
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

  1. -h (human-readable): Displays sizes in KB, MB, GB.
  2. -T (filesystem type): Shows filesystem types like ext4, xfs, etc.
  3. -i (inodes): Displays inode usage instead of disk usage.

Use Cases of df:

Check available space on all mounted filesystems:

df -hT
Enter fullscreen mode Exit fullscreen mode

This helps in monitoring different storage partitions.

Find which partition is nearly full:

df -h | grep -E 'Filesystem|%'
Enter fullscreen mode Exit fullscreen mode

Useful for proactively cleaning up space before a system runs out of storage.

Check inode usage (for small file-heavy servers):

df -i
Enter fullscreen mode Exit fullscreen mode

If inodes are exhausted, no new files can be created even if space is available.


3. Key Differences Between du and df

Image description

Image description

4. Why Do du and df Show Different Values?

Sometimes, du and df may show different results because:

  1. Deleted but open files: If a process is using a deleted file, df still counts its space.
  2. Different mount points: df reports disk-wide usage, while du only shows a selected directory.
  3. 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 .
Enter fullscreen mode Exit fullscreen mode

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!

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay