df Command to Check Disk Space

The df (disk free) command displays information about disk space usage on file systems. It’s useful for monitoring available space and identifying potential issues.

Basic Usage

df

This shows disk usage in 1K blocks. For human-readable format:

df -h

Checking Root Partition Usage Percentage

To get the usage percentage for the root partition:

df -h | awk 'NR==2' | awk '{print $5}'
  • NR==2: Selects the second line (assuming root is first).
  • $5: Prints the 5th column (Use%).

To remove the percentage sign:

df -h | awk 'NR==2' | awk '{print $5}' | tr -d '%'

Important Notes

  • Columns in df output: Filesystem, Size, Used, Avail, Use%, Mounted on.
  • Options:
    • -h: Human-readable (e.g., GB, MB).
    • -T: Include filesystem type.
    • -i: Show inode usage instead of blocks.
  • For specific partitions: df -h / for root, or df -h /home.
  • Monitor regularly: Use in scripts or cron jobs to alert on low space.
  • Related commands: du for directory usage, lsblk for block devices.

Examples

  • Check all filesystems: df -h
  • Check inode usage: df -i
  • Sort by usage: df -h | sort -k5 -hr

References