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,ashatail shows the last lines:
$ tail -n 1 harvest.csv
2026-06-04,basil,0.6,ashaTwo tail tricks are worth memorising:
tail -n +2means "start from line 2", so it prints everything except the header. That's perfect for CSV files.tail -ffollows 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.txtBy 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.
Count everything
wc -l harvest.csvsays 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.
How many lines does head harvest.log print from a 500-line file?
Which command prints harvest.csv without its header line?
Which command keeps printing new lines as a log file grows?
notes.txt has 5 text lines and 2 blank lines. What is the highest number nl notes.txt prints?
Remember
- Peek before you process:
headfor the top,tailfor the bottom,wcto count,nlto number. - Ask "which end, and how many?", and use
tail -n +2to skip a header. tail -n 2is the last two lines,tail -n +2is everything from line 2, andtail -ffollows a live file.
