Skip to the workspace
Pebellslearn by building
Text Processing  ›  Lesson

head, tail, wc and nl — Looking Before You Process

head 12 minhead and tail show the start and end of a file, tail -n +N starts from a line and tail -f follows a growing file, wc counts, and nl numbers lines.

linux / head, tail, wc and nl — Looking Before You Process

Explore each scene at your own pace.

01 · The rule

The rule

head, tail, wc and nl — Looking Before You Process

A log file can have two million lines. Opening it in an editor is slow, and printing all of it floods the terminal. Before doing anything clever with a file, Asha takes a quick look: the top, the bottom, how big it is, and where each line sits. Four small tools do this.

The rule

Peek before you process: head, tail, count, number.

head shows the first lines, 10 by default:

$ head -n 2 harvest.csv
date,crop,kg,picker
2026-06-01,tomato,3.5,asha

tail shows the last lines:

$ tail -n 1 harvest.csv
2026-06-04,basil,0.6,asha

Two tail tricks are worth memorising:

  • tail -n +2 means "start from line 2", so it prints everything except the header. That's perfect for CSV files.
  • tail -f follows a growing file and prints new lines as they're added. Press Ctrl+C to stop. It's the standard way to watch a log live.

wc (word count) counts lines, words and bytes:

$ wc harvest.csv
  7   7 181 harvest.csv
$ wc -l harvest.csv
7 harvest.csv

-l is lines, -w is words (runs of text separated by spaces) and -c is bytes. Each CSV row has no spaces, so here every line counts as one word.

nl numbers lines:

$ nl -ba notes.txt

By default nl skips numbering blank lines. -ba ("body: all") numbers every line. cat -n does much the same.

All four read stdin when you don't give a file, so they fit neatly into pipes.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.