head prints the first lines (default 10) of one or more files or of stdin. It is the quick way to peek at a file without opening it, and the standard way to limit a pipeline's output to a manageable size.
head file.txt # first 10 lines
head -n 20 file.txt # first 20 lines
head -n 1 file.txt # just the first line
head -c 100 file # first 100 bytes
head -n -5 file # all but the last 5 lines (GNU)
tail -n +2 file # skip first line (complement)
sort -rn big.log | head # top 10
head is often paired with tail for extracting a specific range: head -n 20 file | tail -n 10 prints lines 11–20. For more surgical selection, sed -n '11,20p' file does the same thing in one step.
Piping long-running commands through head can also act as a safety valve—find / | head -n 100—terminating the upstream command once it has produced enough output (via SIGPIPE).
Discussed in:
- Chapter 8: Text Processing — Viewing Files
Also defined in: Textbook of Linux