Home / Alt manpages / head(1)

  • head(1)
  • User command
  • linux

Use head to Inspect Files Without Losing the Important Bits

Before you cat a multi-gigabyte log to "just take a quick look", reach for head instead: it prints the first 10 lines by default and stops there. By the end you will know when to select lines, when to select bytes, how to handle several files at once, and how to avoid accidental output changes in scripts.

Prerequisites: a Linux shell and GNU coreutils. The command used for these examples is coreutils 9.4. Nothing here needs elevated privileges unless the file you choose is not readable by your account. Allow about five minutes, plus time to pick a harmless test file.

1. Check the local version and make a small test file

Confirm which implementation you are using first. The options and suffixes below describe GNU head, not an unspecified Unix variant.

$ head --version | head -n 1
head (GNU coreutils) 9.4

For repeatable output, create a temporary file in /tmp. This changes state, but only to create a disposable file; remove it when you finish.

test_file=$(mktemp)
printf '%s\n' alpha bravo charlie delta echo foxtrot > "$test_file"
head "$test_file"
rm -- "$test_file"

Expect the six lines you wrote. A file with fewer than 10 lines prints in full: the default is a maximum, not a promise of exactly 10 lines.

Checkpoint

If you are following along, recreate the temporary file before the next step, because the cleanup command above just removed it.

2. Select a number of lines

Use -n when a line is the unit that matters. The long form reads better in a script:

$ printf '%s\n' one two three four five | head --lines=3
one
two
three

The short form is head -n 3. A bare numeric option such as head -3 still works on this GNU version for compatibility, but the explicit option says what you meant and will not get confused with another argument later.

There is a second line-count mode people miss constantly: a leading hyphen means all but the last count of lines.

$ printf '%s\n' one two three four five | head --lines=-2
one
two
three

Still a preview, not an edit: the input file is untouched. If it has fewer lines than the excluded count, the result is simply empty.

3. Select bytes when lines are the wrong boundary

Use -c or --bytes for a byte count. This matters for binary data, fixed-size records, and pipelines where a newline has no special meaning.

$ printf 'abcdefghij' | head --bytes=4
abcd$

The shell prompt lands on the same line because the input had no trailing newline. That is expected: head does not add one just to tidy up your terminal.

As with lines, a negative count keeps everything except the final count of bytes:

$ printf 'abcdefghij' | head --bytes=-3
abcdefg
  • Suffixes work here too. Decimal kB means 1,000 bytes; K and KiB mean 1,024. head -c 4K file.bin selects 4,096 bytes.
  • Do not call this a character count. Against a UTF-8 text file, four bytes can be fewer than four visible characters, or can stop mid-way through a multi-byte character.

4. Read standard input deliberately

With no file operand, or a file operand of -, head reads standard input, which is what makes it useful at the start of a pipeline.

$ printf '%s\n' first second third fourth | head -n 2
first
second
$ printf '%s\n' first second third | head -n 1 -- -
first

The -- in the second command ends option processing, and the trailing - names standard input explicitly. It is not required there, but it removes any ambiguity once a script grows more arguments.

Do not lean on head as proof that a command completed successfully. A producer can fail after writing just enough data for head to exit happily, and pipeline exit-status rules depend on the shell. Check the producer separately when that status actually matters.

5. Handle multiple files and headers

Hand it more than one file and GNU head prints a header naming each one before its selected content.

$ printf 'red\nblue\n' > /tmp/head-one
$ printf 'green\nyellow\n' > /tmp/head-two
$ head -n 1 /tmp/head-one /tmp/head-two
==> /tmp/head-one <==
red
==> /tmp/head-two <==
green
  • Use -q or --quiet when those headers would corrupt a machine-readable stream.
  • Use -v or --verbose when you want a header even for a single file.
  • The default is the useful middle ground: headers for multiple files, none for one.

These two files are disposable examples. Remove them once you have checked the output:

$ rm -- /tmp/head-one /tmp/head-two

That rm is the only destructive command in this guide. Check the paths before you press Return, and never swap them for a broad wildcard while you are experimenting.

6. Preserve NUL-delimited data

Most text uses newline delimiters, but tools that handle arbitrary file names often use NUL bytes instead. -z or --zero-terminated tells head to treat NUL as the record delimiter rather than newline.

$ printf 'one\0two\0three\0' | head -z -n 2 | od -An -t x1
 6f 6e 65 00 74 77 6f 00

The two records and their terminating NUL bytes survive intact. Without -z, that same stream has no newline at all, so line selection would treat it as one long line. Carry the option through the rest of the pipeline whenever the next command also expects NUL records.

7. Diagnose the common traps

  • Unreadable path. Produces an error and a non-zero status. Check spelling and permissions first, and reach for elevated privileges only when you are authorised to read that file.
  • Filename starting with a hyphen. Can be mistaken for an option. Put -- before it, as in head -n 5 -- --notes.txt.
  • Negative counts are exclusions, not a negative amount of output. They need -n or -c with the count attached or given as its argument.
  • Byte mode can split encoded text. It does not understand records, so choose line mode for human-readable configuration files unless you specifically need a byte boundary.
  • Headers are output, not comments. Suppress them before feeding several files into a parser.

For a final, harmless check, confirm the installed command still reports the release you expect:

$ head --version | sed -n '1p'
head (GNU coreutils) 9.4

Done means

  • Lines and bytes. You can select the first lines with -n and the first bytes with -c.
  • Negative counts understood. You know a negative count excludes data from the end.
  • Pipelines handled. You can read standard input and preserve NUL-delimited records with -z.
  • Headers predictable. You can predict when multiple-file headers appear and suppress them with -q.
  • Cleaned up. You removed the temporary files and did not change any source file.