Posted in

How to Use df and du Commands to Check Disk Space Usage

Efficient disk space management is a fundamental aspect of maintaining a healthy Linux system. Without a clear understanding of where storage is being consumed, administrators risk performance degradation, application failures, and critical data loss. This guide will critically examine the df and du commands, providing a precise, step-by-step approach to monitoring and analyzing disk space usage effectively. By the end, you will confidently identify mounted filesystem usage and determine the exact disk consumption of files and directories, thereby gaining crucial insight into your system’s storage landscape.

Prerequisites

To follow this guide, you will need:

  • Access to a Linux command-line interface (CLI).
  • Basic familiarity with executing commands in the terminal.
  • sudo privileges for examining system-critical directories or filesystems, though most operations can be performed as a regular user.

1. Understand and Utilize the df Command for Filesystem Usage

The df (disk free) command provides a summary of disk space usage for all mounted filesystems. It reports total space, used space, available space, and the percentage of space used, along with the mount point. This is crucial for a high-level overview of your partitions’ health.

Execute a Basic Filesystem Overview

To get a human-readable summary of your mounted filesystems, use the -h (human-readable) option:

df -h

This output will typically show filesystems like /dev/sda1, tmpfs, and udev, indicating their size in gigabytes (G), megabytes (M), or kilobytes (K).

Pro-Tip: Include Filesystem Type

For a more detailed analysis, combine -h with -T (print filesystem type) to see the type of each filesystem (e.g., ext4, xfs, tmpfs):

df -hT

Warning: Understanding df‘s Reporting

It is critical to note that df reports space allocated by the filesystem, not necessarily the sum of individual file sizes. This can lead to discrepancies with du because df accounts for reserved blocks (often for root processes to prevent system lockout), metadata, and sparse files differently than du. Do not mistake df for a file-by-file size calculator; it measures block usage at the filesystem level.

2. Analyze Disk Usage with the du Command for Directories and Files

While df provides a macroscopic view, du (disk usage) offers a microscopic perspective, detailing the disk space consumed by files and directories within a specified path. This is invaluable for pinpointing specific large directories or files.

Check a Directory’s Total Size

To get the summarized size of a specific directory in a human-readable format, use -s (summarize) and -h (human-readable):

du -sh /var/log

This command will output the total disk space occupied by the /var/log directory and all its contents.

Perform a Recursive Directory Scan

To see the size of all subdirectories within a given path, omit the -s option:

du -h /home/youruser/documents

This will list each subdirectory and its size. For very large directories, this output can be extensive.

Pro-Tip: Sorting and Limiting Output

When searching for large culprits, combine du with sort and head. For example, to find the top 10 largest directories in your current path:

du -h . | sort -rh | head -n 10

The . signifies the current directory. sort -rh sorts by human-readable numbers in reverse (largest first).

Warning: Performance and Permissions

Running du on very large directory trees (e.g., du -h /) can be extremely slow and resource-intensive. Always specify a narrower path if possible. Additionally, `du` may report permission denied errors if it encounters directories or files it cannot read. To avoid incomplete results, use sudo when scanning system directories (e.g., sudo du -sh /var/cache).

3. Practical Scenarios and Advanced Usage

Combining these commands allows for more targeted troubleshooting and monitoring.

Identify Major Disk Consumers Across Filesystems

To find the largest directories directly under the root (/) that are consuming space, which often points to logs, caches, or user data:

sudo du -sh /* | sort -rh | head -n 10

This command requires sudo to properly scan system-level directories and will list the top 10 largest first-level directories.

Exclude Specific Filesystem Types from df

Sometimes you want to ignore temporary filesystems (like tmpfs or devtmpfs) from df‘s output, as they are not persistent storage. Use the --exclude-type option:

df -h --exclude-type=tmpfs --exclude-type=devtmpfs

Next Steps

With a solid grasp of df and du, you can maintain a vigilant eye on your system’s disk space. For interactive, ncurses-based disk usage analysis, explore tools like ncdu, which provides a navigable interface for drilling down into directories. Understanding which processes are actively writing to disk can be achieved with tools like lsof or iotop, offering an even deeper level of insight into disk activity.

Leave a Reply

Your email address will not be published. Required fields are marked *