TExcelTAD
Home › Linux & Bash › df, du & free

df, du & free

"The disk is full" and "the server is out of memory" are two of the most common problems on a Linux machine. Three commands answer the first questions: df (how full are the disks?), du (which directories use the space?) and free (how much memory is available?).

df: free space per filesystem

df -h shows each mounted filesystem with human-readable sizes. Sample output (varies by machine):

bash
df -h
text
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p2  468G  301G  144G  68% /
/dev/nvme0n1p1  511M  6.1M  505M   2% /boot/efi
tmpfs           3.9G  2.1M  3.9G   1% /tmp
/dev/sdb1       1.8T  1.2T  540G  70% /mnt/backup
Option Effect
-h Sizes like 144G instead of 1K-blocks
-T Show the filesystem type (ext4, xfs, tmpfs…)
-i Show inodes (number of files) instead of bytes
df -h /var Only the filesystem that contains /var

Note: A disk can be "full" with gigabytes free if it ran out of inodes (millions of tiny files, e.g. a cache or mail queue). Check with df -i.

Monitoring scripts parse df output. Here's a disk-usage alert run against a saved copy of the listing above, so the result is always the same:

bash · try it yourself
cat > df.txt <<'DF'
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p2  468G  301G  144G  68% /
/dev/nvme0n1p1  511M  6.1M  505M   2% /boot/efi
tmpfs           3.9G  2.1M  3.9G   1% /tmp
/dev/sdb1       1.8T  1.7T   80G  96% /mnt/backup
DF
limit=90
awk -v limit="$limit" 'NR > 1 { use = $5; sub("%", "", use)
  if (use + 0 >= limit) printf "ALERT %s is %s%% full\n", $6, use
  else printf "ok    %-12s %3s%%\n", $6, use }' df.txt
ok    /             68%
ok    /boot/efi      2%
ok    /tmp           1%
ALERT /mnt/backup is 96% full

On a real server you'd use df -h | awk ... or df --output=pcent,target.

du: what uses the space

du (disk usage) adds up the sizes of files in directories. The classic way to find what fills a disk:

bash
du -sh /var/* 2>/dev/null | sort -h | tail -n 5
text
95M     /var/cache
210M    /var/lib
1.4G    /var/log
Option Effect
-s Only a total per argument (summary)
-h Human-readable sizes
-d 1 Depth: one line per sub-directory
-b Apparent size in bytes (what the content takes, not the disk blocks)
-c Add a grand total

By default du reports disk blocks used, which depend on the filesystem (a one-byte file still occupies a 4 KiB block). -b reports the bytes of content, which is what this example uses so the numbers are exact:

bash · try it yourself
mkdir -p project/{logs,src,assets}
head -c 50000 /dev/zero > project/logs/app.log
head -c 20000 /dev/zero > project/logs/old.log
head -c 3000 /dev/zero > project/src/main.py
head -c 120000 /dev/zero > project/assets/video.mp4
find project -type f -exec du -b {} + | sort -n
echo "--- biggest first:"
find project -type f -exec du -b {} + | sort -rn | head -n 2
echo "--- total:"
find project -type f -exec cat {} + | wc -c
3000	project/src/main.py
20000	project/logs/old.log
50000	project/logs/app.log
120000	project/assets/video.mp4
--- biggest first:
120000	project/assets/video.mp4
50000	project/logs/app.log
--- total:
193000

head -c N /dev/zero writes N zero bytes: a handy way to make test files of an exact size. sort -n sorts numerically, and for du -h output use sort -h, which understands K, M and G.

free: memory

free -h shows RAM and swap. Sample output (varies):

bash
free -h
text
               total        used        free      shared  buff/cache   available
Mem:           7.8Gi       3.0Gi       2.3Gi       210Mi       2.5Gi       4.5Gi
Swap:          2.0Gi          0B       2.0Gi

The column that matters is available: memory that programs can still get. Linux deliberately fills "free" memory with disk cache (buff/cache) to speed things up and gives it back when needed, so a small "free" value is normal and not a problem.

Situation What it looks like
Healthy available comfortably large, swap barely used
Memory pressure available near 0, swap usage climbing, system slow
Out of memory The kernel's OOM killer ends a process (check dmesg)

The numbers come from the file /proc/meminfo, which scripts can read directly. Here's a small parser on a saved sample:

bash · try it yourself
cat > meminfo.txt <<'MEM'
MemTotal:        8141012 kB
MemFree:         2365848 kB
MemAvailable:    4718392 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
MEM
awk '/^MemTotal/ {t=$2} /^MemAvailable/ {a=$2}
     END { printf "available: %.1f GiB of %.1f GiB (%.0f%%)\n", a/1048576, t/1048576, 100*a/t }' meminfo.txt
available: 4.5 GiB of 7.8 GiB (58%)

Quick disk-full checklist

  1. df -h: which filesystem is full?
  2. du -sh /that/mount/* | sort -h: which directory?
  3. Repeat du one level deeper until you find the culprit (often logs, caches, old Docker images: docker system df).
  4. Deleted a big file but space didn't come back? A running process still has it open: lsof +L1, then restart that process.

Test yourself

Which command shows how full each mounted filesystem is?

  1. du -sh
  2. df -h
  3. free -h
  4. ls -lh /

In free -h output, which column tells you how much memory programs can still use?

  1. free
  2. used
  3. available
  4. shared

Which sort option correctly orders du -h output such as 95M and 1.4G?

  1. sort -n
  2. sort -r
  3. sort -h
  4. sort -k2