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 that gets spotted in advance, and how the directory responsible gets found once it happens.

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

CommandCounts
wc -l fileLines, the form used in unit 5's pipelines
wc -c fileBytes, where a byte is the basic unit of storage and one plain English character, including the invisible newline ending each line, takes one byte

The form wc -c < file uses the input redirection from lesson 5-1 to feed the file to wc through stdin. Written that way, wc prints only the number and does not repeat the filename, which keeps the output easy to pipe onward.

Lines versus bytes on the same file

seq builds a hundred-line file, and wc measures it first in lines, then in bytes, with a one-line file for comparison. The tr -d ' ' strips the padding some systems print around the bare number.

seq 1 100 > numbers.txt
echo "short" > note.txt
wc -l < numbers.txt | tr -d ' '
wc -c < numbers.txt | tr -d ' '
wc -c < note.txt | tr -d ' '

Output

100
292
6

The 292 is worth taking apart, because it shows that bytes count characters rather than lines. The numbers 1 through 9 occupy two bytes each once their newline is included, 10 through 99 take three, and 100 takes four, and those groups sum to 292. The same logic explains the 6, since short is five characters plus the newline that echo appends.

Whole directories and whole disks

Two more tools scale the question up from single files:

CommandNameReports
du -sh directorydisk usageHow much space the directory and everything inside it occupies. -s prints one summary number instead of a line per subdirectory, and -h makes it human-readable, so 1.4G rather than a raw block count.
df -hdisk freeOne line per disk with its total size, space used, space available, and where it is attached.

They answer opposite questions. df reports how full each disk is, and du reports who is filling it. The classic full-disk hunt alternates between them, narrowing by one directory level each time:

df -h              # confirm which disk is nearly full
du -sh /var/*      # find the huge directory inside it
du -sh /var/log/*  # go a level deeper and repeat

Exact sizes differ on every machine, which is why this sequence is shown rather than given with fixed output. The shape of the hunt is the part worth remembering.

When a server fails with No space left on device, the first command to run is df -h. It reports every disk's total, used, and available space, which confirms which disk actually filled up before any time is spent guessing.

The other tools come later or answer smaller questions. du locates which directories are consuming the space once the disk is known, wc measures individual files, and ls -l shows per-file sizes for a single directory only.

Byte counts make the size difference between two files concrete. seq 1 20 > big.txt writes twenty numbers, echo "hi" > small.txt writes a single short word, and the redirect form of wc -c reports each file's size as a bare number.

seq 1 20 > big.txt
echo "hi" > small.txt
wc -c < big.txt | tr -d ' '
wc -c < small.txt | tr -d ' '

Output

51
3

Both numbers include the newline at the end of every line. big.txt holds 1 through 9 at two bytes each plus 10 through 20 at three bytes each, giving 51, and small.txt holds two letters plus one newline, giving 3. Using wc -c < file rather than wc -c file is what keeps the filename out of the output so tr receives only the digits.