Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Turn the question into "which end, and how many?" The start means head, the end means tail, "everything except the first few" means tail -n +N, and "how many" means wc -l.

How many harvest records are there?Step 1 of 4
  1. Count everything

    wc -l harvest.csv says 7.

The trap is tail -n 2 versus tail -n +2. Without the plus you get the last two lines. With it you get everything from line 2 onward.

Drills

Answer fast — trust the rule.

quick drill

How many lines does head harvest.log print from a 500-line file?

quick drill

Which command prints harvest.csv without its header line?

quick drill

Which command keeps printing new lines as a log file grows?

quick drill

notes.txt has 5 text lines and 2 blank lines. What is the highest number nl notes.txt prints?

Remember

  • Peek before you process: head for the top, tail for the bottom, wc to count, nl to number.
  • Ask "which end, and how many?", and use tail -n +2 to skip a header.
  • tail -n 2 is the last two lines, tail -n +2 is everything from line 2, and tail -f follows a live file.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainCount the records

Explain how to find how many harvest records are in harvest.csv, which has a header line, and explain the difference between tail -n 2 and tail -n +2. Also say how you would watch a log file as it grows.