Automating Disk Monitoring Using DF (Disk Free) in Scripts

DF (Disk Free) Command: Practical Examples and OptionsThe df (disk free) command is a standard Unix/Linux utility that displays information about the amount of disk space available on mounted file systems. It’s a simple but essential tool for system administrators, developers, and anyone managing storage on Unix-like systems. This article covers the basics, common options, practical examples, interpretation of output, scripting use, and troubleshooting tips.


What df Shows

The df command reports the total size, used space, available space, and mount points for each mounted filesystem. By default, sizes are shown in 1K blocks on many systems, though behavior can vary.

Key fields typically shown by df output:

  • Filesystem — the device or virtual filesystem name.
  • 1K-blocks / Size — total size of the filesystem.
  • Used — space used.
  • Available / Avail — space available to non-privileged users.
  • Use% — percentage of space used.
  • Mounted on — mount point directory.

Common Options

  • -h, –human-readable
    Show sizes in human-readable format (e.g., 1K, 234M, 2G).
  • -H
    Same as -h but uses powers of 1000 (SI units) instead of 1024.
  • -k
    Show sizes in kilobytes (1K blocks).
  • -m
    Show sizes in megabytes.
  • -T, –print-type
    Print filesystem type (e.g., ext4, tmpfs).
  • -a, –all
    Include pseudo, duplicate, inaccessible file systems.
  • –total
    Display a grand total.
  • -i, –inodes
    Show inode usage instead of block usage.
  • -x, –exclude-type=TYPE
    Exclude filesystems of the specified type.
  • –output[=FIELD_LIST]
    Customize displayed fields (GNU df). For example: –output=source,size,used,avail,pcent,target

Practical Examples

  1. Basic usage — default output

    df 

    Shows mounted filesystems with sizes in 1K blocks (on many systems).

  2. Human-readable sizes

    df -h 

    Output:

  • /dev/sda1 50G 20G 28G 42% /
  • tmpfs 2.0G 0 2.0G 0% /run
  1. Show filesystem types

    df -Th 

    Adds a TYPE column (e.g., ext4, tmpfs).

  2. Show only a specific mount point or path

    df -h /home 

    Useful to check space for a single directory or mounted filesystem.

  3. Show inode usage

    df -i 

    Checks whether you’re out of inodes (which would prevent new files from being created even if space exists).

  4. Exclude types (e.g., tmpfs)

    df -h -x tmpfs -x devtmpfs 

    Hides ephemeral filesystems from the output.

  5. Custom output (GNU df)

    df --output=source,size,used,avail,pcent,target -h 

    Selects only fields you care about.

  6. Grand total

    df -h --total 

    Shows a final line summing sizes across filesystems.

  7. Using df in a script to warn on low space

    #!/bin/bash THRESH=90 df -H | awk 'NR>1 {print $5 " " $6}' | while read output; do usep=$(echo $output | awk '{print $1}' | tr -d '%') partition=$(echo $output | awk '{print $2}') if [ $usep -ge $THRESH ]; then echo "Warning: $partition is ${usep}% full" fi done 

Interpreting Common Confusions

  • Difference between “Available” and free space: root can reserve blocks; “Available” shows what non-root users can use.
  • df vs du: df reports filesystem-level usage; du reports directory/file-size usage. Discrepancies occur with deleted-but-open files, reserved blocks, or mount bind/overlay setups.
  • Block size differences: df default blocks can vary; use -h, -k, or –block-size for consistency.

Tips and Best Practices

  • Use df -h when checking quickly; use df -i to diagnose inode exhaustion.
  • Exclude tmpfs and devtmpfs to focus on persistent storage.
  • Combine df with ncdu or du for drill-down analysis.
  • Schedule regular disk checks in monitoring systems (Prometheus node_exporter, Nagios, Zabbix) rather than relying solely on manual df checks.
  • When scripting, parse df output using –output where available to avoid brittle column-based parsing.

Troubleshooting Scenarios

  • “df shows full, but du shows less”: likely deleted-open files or reserved blocks; lsof +L1 can show deleted open files.
  • Unexpected filesystem type: check /etc/mtab and /proc/mounts for mount details.
  • NFS showing odd numbers: client/server timeouts or stale mounts can affect reported sizes; remount or check server export options.

Summary

df is a straightforward command that provides essential filesystem usage information. Knowing its options—especially human-readable output (-h), inode reporting (-i), type inclusion (-T), and exclusion (-x)—makes it a flexible tool for daily maintenance, scripting, and troubleshooting. Use df together with du, lsof, and system monitoring to get a complete picture of storage health.

Comments

Leave a Reply

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