Viewing files
head
Print the first lines (or bytes) of files; 10 lines by default.
head [OPTION...] [FILE...]
head prints the beginning of each file: the first 10 lines unless you ask for a different
number with -n, or a number of bytes with -c. It stops reading as soon as it has what it
needs, so head on a multi-gigabyte file returns instantly.
It is the standard way to peek at a CSV header, check the format of a log, or cut a pipeline's
output down to the top few results (sort | uniq -c | sort -rn | head). Its partner tail
prints the end instead. GNU head also accepts a negative count, -n -3, meaning "everything
except the last 3 lines".
Examples
Show the first 10 lines
head access.log
192.0.2.10 - - [14/Mar/2026:08:01:12 +0000] "GET / HTTP/1.1" 200 5120 "-" "Mozilla/5.0 (X11; Linux x86_64)" 192.0.2.10 - - [14/Mar/2026:08:01:13 +0000] "GET /static/app.css HTTP/1.1" 200 1840 "https://example.com/" "Mozilla/5.0 (X11; Linux x86_64)" 198.51.100.7 - - [14/Mar/2026:08:03:44 +0000] "GET /login HTTP/1.1" 200 2310 "-" "curl/8.5.0" 198.51.100.7 - - [14/Mar/2026:08:03:45 +0000] "POST /login HTTP/1.1" 401 112 "-" "curl/8.5.0" 198.51.100.7 - - [14/Mar/2026:08:03:46 +0000] "POST /login HTTP/1.1" 401 112 "-" "curl/8.5.0" 203.0.113.42 - - [14/Mar/2026:08:05:02 +0000] "GET /api/users HTTP/1.1" 200 734 "-" "python-requests/2.31" 203.0.113.42 - - [14/Mar/2026:08:05:09 +0000] "GET /api/users/7 HTTP/1.1" 404 58 "-" "python-requests/2.31" 192.0.2.10 - - [14/Mar/2026:08:07:30 +0000] "GET /about HTTP/1.1" 200 3302 "https://example.com/" "Mozilla/5.0 (X11; Linux x86_64)" 192.0.2.55 - - [14/Mar/2026:08:09:15 +0000] "GET / HTTP/1.1" 200 5120 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_3)" 192.0.2.55 - - [14/Mar/2026:08:09:16 +0000] "GET /favicon.ico HTTP/1.1" 404 58 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_3)"
With no option head prints 10 lines. access.log has 20, so this is the first half.
Choose how many lines
head -n 3 data/users.csv
id,name,email,dept,joined 1,Alice Moreau,alice@example.com,engineering,2021-04-12 2,Bob Okafor,bob@example.com,sales,2019-11-03
-n 3 prints the header row and the first two records, a quick way to learn a CSV's columns.
Print just the first line
head -n 1 data/sales.csv
date,region,product,units,price
One line is all a CSV header needs. head -1 is an older spelling of the same thing that GNU still accepts.
Everything except the last lines
head -n -3 todo.txt
buy milk fix login bug
A negative count drops that many lines from the end. The file has 5 lines, so the first 2 are printed. This form is GNU-only.
Print the first bytes
head -c 16 README.md; echo
# demo A small
-c 16 prints exactly 16 bytes, here # demo, a blank line and the start of the next
sentence, cut mid-word. The trailing echo adds the newline the cut removed.
Several files get headers
head -n 2 todo.txt notes.txt
==> todo.txt <== buy milk fix login bug ==> notes.txt <== Meeting notes, March - Move backups to the new server
With more than one file, head prints a ==> name <== header before each and a blank
line between them.
Several files without headers
head -q -n 1 logs/app.log logs/app.log.1 logs/error.log
2026-03-14 08:00:01 INFO server started on port 8080 2026-03-13 23:58:12 INFO nightly backup started [2026-03-14 08:16:40] database timeout after 5000ms (host=db01)
-q suppresses the headers, giving the first line of each file as one plain list.
Keep the top results of a pipeline
cut -d ' ' -f 1 access.log | sort | uniq -c | sort -rn | head -n 3
5 192.0.2.10
4 203.0.113.42
4 198.51.100.7 The classic "top N" pipeline: count requests per client IP, sort the counts from highest to
lowest, and let head keep the three busiest clients.
Print a range of lines
head -n 5 data/numbers.txt | tail -n +3
19 3 100
head -n 5 keeps lines 1 to 5 and tail -n +3 starts that output at its third line, so
lines 3, 4 and 5 (19, 3, 100) remain.
A missing file
head -n 1 todo.txt nope.txt
==> todo.txt <== buy milk head: cannot open 'nope.txt' for reading: No such file or directory
head prints what it can, reports the missing file on standard error and exits with 1.
Common pitfalls
head -n -N(all but the last N) is a GNU extension; BSD/macOSheadrejects negative counts. Portable alternative:sed '$d'for one line, orawkwith a buffer.With
set -o pipefail,producer | head -n 1can fail:headexits early and the producer may get SIGPIPE (status 141). It is harmless in most pipelines, but it trips strict-mode scripts.head -ccounts bytes, not characters: cutting UTF-8 text at a byte count can split a multi-byte character.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
Negative counts (
-n -3,-c -10) and suffixes like-c 1Kare GNU-only.
Options worth knowing
A selection, described in our own words. For the full list, see head(1) or head --help.
| Option | What it does |
|---|---|
-n, --lines [-]NUM | Print the first NUM lines; with a leading minus, all but the last NUM. |
-c, --bytes [-]NUM | Print the first NUM bytes; with a leading minus, all but the last NUM. Accepts suffixes like K and M. |
-q, --quiet | Never print file name headers. |
--silent | Same as --quiet. |
-v, --verbose | Always print file name headers, even for one file. |
-z, --zero-terminated | Lines end with a NUL byte instead of newline. |
Related
tailPrint the last lines of files, or follow a file as it grows with -f.catConcatenate files and print them to standard output; also the quickest way to see a short file.lessPage through a file or command output one screen at a time, with search and scrolling in both directions.sedEdit a stream of text with a script of commands, most often to substitute text or delete lines.- Pipes and pipelinesHow cmd1 | cmd2 connects processes, which exit status a pipeline returns, PIPESTATUS, pipefail, SIGPIPE, and why variables set in a piped loop vanish.