opscape

Viewing files

head

Print the first lines (or bytes) of files; 10 lines by default.

head [OPTION...] [FILE...]

10 of 10 examples run in Debian 13.7 on 2026-10-31 · coreutils 9.7-3 · manual: head(1)

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
explain
output
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
explain
output
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
explain
output
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
explain
output
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
explain
output
# 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
explain
output
==> 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
explain
output
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
explain
output
      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
explain
output
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
explain
outputexit 1
==> 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/macOS head rejects negative counts. Portable alternative: sed '$d' for one line, or awk with a buffer.

  • With set -o pipefail, producer | head -n 1 can fail: head exits early and the producer may get SIGPIPE (status 141). It is harmless in most pipelines, but it trips strict-mode scripts.

  • head -c counts 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 1K are GNU-only.

Options worth knowing

A selection, described in our own words. For the full list, see head(1) or head --help.

OptionWhat it does
-n, --lines [-]NUMPrint the first NUM lines; with a leading minus, all but the last NUM.
-c, --bytes [-]NUMPrint the first NUM bytes; with a leading minus, all but the last NUM. Accepts suffixes like K and M.
-q, --quietNever print file name headers.
--silentSame as --quiet.
-v, --verboseAlways print file name headers, even for one file.
-z, --zero-terminatedLines end with a NUL byte instead of newline.

Related