Linux head Command: Display the Beginning of a File
The head command prints the beginning of a file or standard input. It defaults to the first ten lines and is useful for checking a header, sampling data, or limiting the visible portion of pipeline output.
This guide explains the basic syntax, key options, practical examples, related commands, common cautions, and frequently asked questions for head.
What Is the head Command?
Use -n to select a number of lines and -c to select bytes. With multiple files, a filename header normally identifies each result. GNU head also supports negative counts that mean all input except the final number of lines or bytes.
Basic Syntax
head [OPTION]... [FILE]...
Options and edge cases can vary by implementation and version. Check head --help and man head on the target system before relying on implementation-specific behavior.
Basic head Usage
Show the first ten lines
With no count option, head prints ten lines from each input.
head access.log
Choose a line count
Pass the required number to -n.
head -n 25 access.log
Choose a byte count
The count is bytes, not Unicode characters, so a multibyte character can be split.
head -c 64 sample.bin
Results of reading only the beginning
head -n 2 prints only the first two lines from the input. It is useful for checking small samples without changing the original.
printf 'one\ntwo\nthree\n' | head -n 2
Example output:
one two
If the command before the pipe produces a lot of data, head may terminate early, causing a broken pipe error in the preceding command. In automation, be sure to check the exit status of the entire pipeline.
Key Options
| Option | Purpose |
|---|---|
-n NUM |
Print the first NUM lines. |
-c NUM |
Print the first NUM bytes. |
-q |
Suppress filename headers for multiple inputs. |
-v |
Always print filename headers. |
-n -NUM |
On GNU systems, print all but the last NUM lines. |
-c -NUM |
On GNU systems, print all but the last NUM bytes. |
Practical Examples
Inspect a CSV header and sample
View the column header and a few records without opening the complete data set.
head -n 6 sales.csv
Compare beginnings of several logs
Force filename headers so the source of each section remains clear.
head -v -n 5 app.log worker.log
Limit a command result
The producer may receive SIGPIPE after head has enough input, which matters when interpreting a strict pipeline's status.
find /var/log -type f | head -n 20
head and Related Commands
| Command | Primary role | Best fit |
|---|---|---|
head |
Print the beginning | Headers and samples |
tail |
Print or follow the end | Recent log entries |
sed |
Select and transform ranges | More complex line processing |
less |
Browse the complete input | Interactive search |
Important Cautions
Byte counts are not character counts
A UTF-8 character can be cut between bytes and appear malformed.
Head preserves the existing order
Sort first if the required result is based on value, size, or time rather than current order.
Check pipeline exit semantics
With pipefail, an upstream SIGPIPE may produce a nonzero pipeline status even when head got the requested lines.
FAQ
How many lines does head print by default?
Ten lines.
How do I print only the first line?
Use head -n 1 FILE.
Which command prints the final lines?
Use tail.
Can I print the first 1 KiB?
GNU head accepts head -c 1K FILE; verify suffix support on other implementations.









