Disk Space: df & du

df shows free space on your disks; du shows how much space files and folders use.

Syntaxdf -h du -sh [path]

Running out of disk is a common server problem. Two commands diagnose it.

df β€” free space per disk

df -h lists each mounted filesystem with its size, used and available space. The -h gives human-readable units. Watch the Use% column.

du β€” space used by files

du reports how much space specific files and folders occupy. du -sh gives a single total for a folder.

Together they answer "the disk is full β€” what is filling it?"

Example

Example Β· bash
# Free space on each disk
df -h
# Filesystem  Size Used Avail Use% Mounted on
# /dev/sda1    40G  22G   16G  59% /

# Total size of a folder
du -sh /var/log
# 512M    /var/log

# Biggest subfolders here, sorted
du -sh * | sort -h

When to use it

  • A sysadmin runs 'df -h' to check disk usage across all mounted filesystems before a large database migration.
  • A developer uses 'du -sh /var/log/*' to identify which log files are consuming the most space on a nearly full server.
  • A DevOps engineer adds 'df -h | awk NR>1{if($5+0>80)print $0}' to a monitoring script to alert when any filesystem exceeds 80% usage.

More examples

Check filesystem disk usage

Shows df -h for human-readable sizes across all mounted filesystems, including Use% which triggers alerts.

Example Β· bash
df -h
# Filesystem      Size  Used Avail Use% Mounted on
# /dev/sda1        50G   32G   16G  67% /
# /dev/sdb1       200G  145G   45G  77% /var

Find large directories

Uses du -sh with a glob to measure directory sizes and sort -rh to rank them largest first.

Example Β· bash
# Show size of each item in /var
du -sh /var/*
# 1.2G  /var/cache
# 3.8G  /var/log
# 512M  /var/lib

# Find top 10 largest directories from /
du -sh /* 2>/dev/null | sort -rh | head -10

Alert on high disk usage

Pipes df into awk to filter and print only filesystems whose Use% exceeds the threshold, ready for a cron alert.

Example Β· bash
#!/bin/bash
# Alert if any filesystem exceeds 85% usage
df -h | awk 'NR>1 && $5+0 > 85 {
  print "ALERT: " $6 " is at " $5
}'

Discussion

  • Be the first to comment on this lesson.
Disk Space: df & du β€” Linux | SoundsCode