Master the df -h Command: Monitor Disk Usage in Linux

Check disk space in a human-readable format with df -h!

What is the df -h Command?

Imagine df -h as a health check for your system’s storage, displaying disk usage in a human-readable format (e.g., GB, MB). It’s essential for managing disk space on servers or desktops.

Why df -h is Essential

Disk Space Monitoring

Track available storage across filesystems.

Server Management

Prevent disk full errors on servers.

Automation

Script alerts for low disk space.

Syntax and Options

The df -h command shows disk usage:

df -h
        

Key options:

  • -h: Human-readable sizes (e.g., GB, MB).
  • -T: Show filesystem type.
  • --exclude-type: Exclude specific filesystem types.
  • -i: Display inode information.

Output columns: Filesystem, Size, Used, Avail, Use%, Mounted on.

Real-World Examples

1. Check Disk Usage

View disk space:

df -h
        

Output: Lists filesystems with sizes in GB/MB (e.g., /dev/sda1 100G 50G 50G 50% /).

2. Show Filesystem Types

Include filesystem type:

df -hT
        

Output: Adds Type column (e.g., ext4, tmpfs).

3. Check Specific Filesystem

View a single filesystem:

df -h /dev/sda1
        

Output: Shows usage for /dev/sda1 only.

4. Exclude Filesystem Types

Skip tmpfs filesystems:

df -h --exclude-type=tmpfs
        

Output: Excludes tmpfs filesystems from the list.

5. Check Inodes

View inode usage:

df -i
        

Output: Shows inode counts instead of disk space.

Advanced Usage

Enhance df -h with these techniques:

  • Scripting Alerts: Use in scripts to warn about low disk space.
  • Sorting Output: Pipe to sort for custom ordering (e.g., by usage).
  • Monitoring Mounts: Combine with watch for real-time updates.
  • Custom Filtering: Use grep to focus on specific mounts.

Example: Alert for low space:

df -h | awk '$5 > 90 {print $6 " is over 90% full!"}'
        

Pro Tips

Check Inodes: Use -i to diagnose file count issues.

Automate Monitoring: Use watch df -h for continuous checks.

Exclude Clutter: Skip tmpfs for cleaner output.

Common Mistakes to Avoid

Avoid these pitfalls with df -h:

  • Ignoring Inodes: Check -i if space is available but writes fail.
  • Missing Mounts: Verify all filesystems are mounted.
  • Misreading Units: Ensure -h is used for readable sizes.

Explore More Linux Commands

Master Linux with our expert guides!