Lädt...

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


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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 :

  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

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:

  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

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

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 .

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!

...

🔧 The Difference Between Day-0, Day-1, and Day-2 Operations


📈 30.08 Punkte
🔧 Programmierung

🔧 Tìm Hiểu Về RAG: Công Nghệ Đột Phá Đang "Làm Mưa Làm Gió" Trong Thế Giới Chatbot


📈 28.58 Punkte
🔧 Programmierung

🔧 Grok 3: AI Thông Minh Nhất Thế Giới


📈 28.58 Punkte
🔧 Programmierung

🕵️ Kèo Thẻ Phạt Vip66 Là Gì? 3 Lối Đánh Kèo Chậm Mà Chắc


📈 28.58 Punkte
🕵️ Reverse Engineering

🔧 KISS Principle: Giữ Mọi Thứ Đơn Giản Nhất Có Thể


📈 28.58 Punkte
🔧 Programmierung

🔧 Có thể bạn chưa biết (Phần 1)


📈 28.58 Punkte
🔧 Programmierung

🐧 What the difference between a Linux distro and a unix-based OS?


📈 20.73 Punkte
🐧 Linux Tipps

🐧 Is there a big difference between Intel and AMD perfomance with Linux?


📈 20.73 Punkte
🐧 Linux Tipps

📰 Difference between Linux P2V and Windows P2V


📈 20.73 Punkte
📰 Alle Kategorien

📰 Linux: Difference Between /dev/tty, /dev/tty0, and /dev/console


📈 20.73 Punkte
🐧 Unix Server

🐧 Difference between an LTS linux kernel, and just staying on the newest kernel that works


📈 20.73 Punkte
🐧 Linux Tipps

🐧 Real Difference Between AMD and Nvidia GPUs on Linux?


📈 20.73 Punkte
🐧 Linux Tipps

🐧 Difference Between the macOS and Linux Kernels


📈 20.73 Punkte
🐧 Linux Tipps

🔧 Learning AWS Day by Day - Day 53 - Differences between SQS, SNS and Amazon MQ


📈 20.23 Punkte
🔧 Programmierung

🔧 Difference Between DevOps and Full-Stack Engineer: Roles, Responsibilities, and Key Skills


📈 19.91 Punkte
🔧 Programmierung

🔧 Design Pattern : difference between composition and inheritance and decorator.


📈 19.91 Punkte
🔧 Programmierung

🔧 The Difference between Each Value in a Certain Column and Its Previous One and Display Result


📈 19.91 Punkte
🔧 Programmierung

🔧 Difference between main branches and master branches in #git and #github


📈 19.91 Punkte
🔧 Programmierung

🔧 Difference Between Factories and Seeders in Laravel: Purpose and Usage


📈 19.91 Punkte
🔧 Programmierung

🔧 Difference Between Local Storage And Session Storage and cookies in the browser


📈 19.91 Punkte
🔧 Programmierung

🐧 Difference in Speeds between SSD and HDD is insane!! Maybe make your root and home partition both in SSD.


📈 19.91 Punkte
🐧 Linux Tipps

🐧 Bash Execution Tips: the difference between &&, &, ; and || and a test teaser


📈 19.91 Punkte
🐧 Linux Tipps

🪟 The difference between Windows Notepad and WordPad, and when to use each


📈 19.91 Punkte
🪟 Windows Tipps

🐧 difference between apache and tomcat, and stopping those services in particular order


📈 19.91 Punkte
🐧 Linux Tipps

📰 Multiple Firewalls, and the Difference Between Router and Computer Firewalls?


📈 19.91 Punkte
📰 IT Security Nachrichten

matomo