Course outline · 0% complete

0/29 lessons0%

Course overview →

How big is it? wc, du, and df

lesson 6-3 · ~8 min · 18/29

Measuring files and disks

No space left on device is a real failure that takes down real servers: logs grow quietly until the disk fills, and then everything on the machine starts erroring at once. Measuring tools are how you see it coming — and, when it happens, how you find the directory responsible.

Start with a single file. wc (word count) measures whatever it's given:

  • wc -l file: count lines (you met this in unit 5's pipelines)
  • wc -c file: count bytes — a byte is the basic unit of storage, and one plain English character (including the invisible newline at each line's end) takes one byte

The form wc -c < file (lesson 5-1's input redirection) feeds the file to wc's stdin, so wc prints just the number without repeating the filename.

Code exercise · bash

Run it. seq builds a 100-line file; wc measures it in lines, then in bytes, then a one-line file for comparison. (tr -d ' ' strips the padding some systems print around the bare number.)

Whole directories and whole disks

  • du -sh directory: disk usage — how much space the directory and everything inside it takes. -s prints one summary number instead of every subdirectory; -h makes it human-readable (1.4G instead of a raw block count).
  • df -h: disk free — one line per disk with its size, space used, space available, and where it's attached.

They answer opposite questions: df says "how full is each disk", du says "who is filling it". The classic full-disk hunt alternates them, narrowing one level at a time:

df -h              # confirm: which disk is at 98%?
du -sh /var/*      # which directory in there is huge?
du -sh /var/log/*  # go a level deeper, repeat

Exact sizes differ on every machine, which is why this part is shown rather than run — the shape of the hunt is the thing to remember.

Quiz

A server fails with "No space left on device". Which command do you run FIRST to see how full each disk is?

Code exercise · bash

Your turn. Make `big.txt` hold the numbers 1 through 20 (seq and >), make `small.txt` hold just the word `hi`, then print each file's byte count using the `wc -c < file | tr -d ' '` form — big.txt first. Expected output: ``` 51 3 ```