Command output can fill a terminal, making its opening harder to inspect. The Linux head command prints the initial part of that output or a file.
This article explains how to set line and byte counts, then combine head with tail to select a line range.
TL;DR: Preview the beginning of a file
Without a count option, head prints its default number of lines. Use -n or -c when the boundary should be explicit.
- head file.txt prints the beginning of the file.
- Use head -n to set a line limit.
- Use head -c to set a byte limit.
- command | head -n 5 limits piped output.
What does the head command do in Linux?
head prints an initial portion of input. It reads named files, or standard input when no filename is given or the filename is a hyphen.
GNU head uses a default line count when you omit a count option, with each newline marking the end of a line.
Byte mode counts raw bytes and can stop inside a multibyte character. GNU Coreutils also accepts negative counts and size suffixes.
How to use the Linux head command
Choose the input first, then decide whether the boundary should count lines or bytes. These examples use GNU/Linux syntax documented by GNU Coreutils.
Step 1: Print the default number of lines
Pass a filename to inspect its beginning. GNU head applies its default when no count option is present.
head file.txt
Step 2: Set a line or byte count
Use -n when the boundary follows lines. Choose -c when it follows bytes instead.
head -n 5 file.txt
head -c 20 file.txt
GNU count values accept size suffixes. K means 1024 bytes, while KB means 1000 bytes. Newlines count toward a byte limit.
Step 3: Read standard input or a pipeline
With no filename, head reads standard input. You can also use a hyphen as the filename.
printf 'red\ngreen\nblue\n' | head -n 2
head -n 2 -
Both commands read from standard input here. The pipe sends printf’s output to head.
Step 4: Process multiple files and label their output
GNU head adds a filename header when there are multiple inputs. The -q option suppresses headers, while -v forces them even for one file.
head -n 2 first.txt second.txt
head -q -n 2 first.txt second.txt
Step 5: Combine head and tail for a line range
To select a middle range, let head stop at the range’s end, then let tail remove the lines before its start. The two counts define the range.
head -n 10 file.txt | tail -n 5
For another range, set the head count to its ending line and the tail count to the number of lines in the range.
What should you know about head’s limits?
head is a preview tool, and its count semantics depend on the option. Check the input and implementation when output is surprising.
| Case | What happens | What to check |
|---|---|---|
| File has fewer lines than -n | Available lines are printed | Confirm the file’s contents |
| -c ends mid-character | Output may contain an incomplete encoded character | Use -n when records are text lines |
| Negative -n or -c | GNU head omits that many lines or bytes from the end | Do not assume this extension on every Unix implementation |
| No filename | head reads standard input until enough input is available or the stream ends | Pipe data in or finish terminal input with EOF |
A negative count tells GNU Coreutils head to omit input from the end, while a head-and-tail pipeline selects a middle range.
Use the tail command in Linux when the task starts from the end of a file. For reading an entire file or paging through it, see cat and more in Linux.
Conclusion: Choose lines or bytes
Use -n for line boundaries and -c for byte boundaries. Check GNU Coreutils documentation when relying on negative counts or suffixes.
GNU’s head manual lists the complete option behavior, including headers and count suffixes.
FAQ
These answers cover the common count and input questions.
What does head do by default?
It prints the first 10 lines of each input file.
Can head read piped input?
Yes. With no file operand, head reads standard input, including output sent through a pipe.
How do you print a fixed number of bytes?
Use head -c followed by the byte count, such as head -c 20 file.txt.
