Managing disk space is a crucial task for any Linux user or admin. Two command-line tools, df and du, make it easy to monitor disk usage. While they both deal with space, they serve different purposes. Here’s how to use them effectively.

df: Disk Free Overview

The df command provides a high-level summary of disk space usage across mounted filesystems. It shows total, used, and available space—perfect for a quick system-wide check.

Basic Usage:

df -h
  • -h: Displays sizes in a human-readable format (e.g., GB, MB).
  • Output includes filesystem name, size, used space, available space, and mount point.

Example output:

df -h output

Check a Specific Filesystem:

To focus on, say, the root directory:

df -h /

Example output:

du: Disk Usage Details

While df gives the big picture, du drills down into specific directories or files, showing how much space they consume. It’s ideal for finding space hogs.

Basic Usage:

Check the size of a directory (e.g., /home):

du -sh /home
  • -s: Summarizes the total size.
  • -h: Human-readable output.

Example output:

Du -sh output

List Sizes of Subdirectories:

du -h /var | sort -hr | head -n 20
  • sort -hr: Sorts by size (human-readable, descending).
  • head -n 20: List only the first 20, since there can be many hundreds of sub directories. This gives you the 20 largest ones.

Example output:

df -h /var output

Key Differences

  • Scope: df reports on entire filesystems; du focuses on specific files or directories.
  • Output: df is a snapshot; du calculates usage recursively.
  • Use Case: Use df to check free space, du to investigate what’s taking up space.

Practical Tips

  • Combine them: Use df -h to spot a full filesystem, then du -sh * in the mount point to identify culprits.
  • Watch for permissions: du may underreport if you lack access to some directories (run with sudo if needed).

Conclusion

Mastering df and du gives you full control over disk space in Linux. Use df for a quick health check and du to dig deeper. Together, they’re your go-to tools for keeping your system lean and efficient.

Power Your Projects with vpszen.com VPS Solutions

Looking for reliable hosting to run your Linux servers and host your next big project? VpsZen.com has you covered with top-tier VPS options tailored to your needs.
Choose from ARM64 VPS Servers for energy-efficient performance, or Root VPS Servers for virtual servers with dedicated resources.