kmail.at
← learning

shell · difficulty ◆

head — show the first lines of a file

Peek at the top of any file or command output without reading it all.

2026-08-22 · 3 min read

$ head

What it does

head prints the first 10 lines of a file (or of anything piped into it) to the terminal. It is the mirror image of tail, which shows the end. Because it stops after a few lines, head is instant even on enormous files — it never reads more than it needs to.

Why it matters

head is the fastest way to get your bearings on an unfamiliar file: check the header of a CSV, the first lines of a log, or the top of a config. Combined with pipes, it also caps the output of any command — `command | head` shows just the first screenful instead of flooding the terminal.

Example

$ head -n 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin

-n 3 limits output to the first 3 lines. Without -n, head shows 10 lines by default.

$ ls -lh | head -5
total 24
-rw-r--r-- 1 kmail kmail 4.2M Aug 22 09:00 server.log
-rw-r--r-- 1 kmail kmail 1.1K Aug 22 09:01 notes.md
-rwxr-xr-x 1 kmail kmail  567 Aug 22 09:02 run.sh
drwxr-xr-x 2 kmail kmail 4096 Aug 22 09:03 src

Piping into head caps any command's output to the first 5 lines — handy when a listing is long.

Common flags

-n N
print the first N lines instead of the default 10
-c N
print the first N bytes instead of lines
-q
quiet — suppress the per-file headers when given multiple files
-v
always print a header showing the filename
-z
treat input as NUL-separated instead of newline-separated

History

Origin

head has been part of Unix since the early days, appearing in the 1970s alongside tail. Its name is simply descriptive — it shows the head (beginning) of a file. It is one of the smallest and oldest members of the coreutils family, and its behavior has barely changed in fifty years.

Fun fact

head and tail are the two halves of the same idea, and they are often used together in pipelines — for example, `head -n 100 file | tail -n 10` shows lines 91 through 100. head is also the tool behind the common habit of previewing a file before committing to a full read.

Related commands

← all learning