head
Print the first lines of a file
By CMD Script Team · 3 min read · Last updated
head [OPTIONS] [FILE...]Options
| Flag | Description |
|---|---|
-n NUM | Print the first NUM lines instead of the default 10 |
-c NUM | Print the first NUM bytes instead of lines |
-n -NUM | Print all but the last NUM lines of the file |
-q | Never print filename headers when given multiple files |
-v | Always print filename headers, even for a single file |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
head prints the beginning of one or more files — by default the first 10 lines. It's
the natural counterpart to tail, and it's especially handy for quickly previewing large
files, checking headers or column names in data files, or trimming down the output of a
long-running command in a pipeline.
Beginner examples
head file.txt— print the first 10 lineshead -n 5 file.txt— print the first 5 lineshead -n 1 file.csv— print just the header row of a CSVhead file1.txt file2.txt— print the first 10 lines of each, with filename labels
head -n 20 access.log
Advanced examples
- Preview raw bytes rather than lines, useful for binary files:
head -c 64 image.png - Print everything except the last N lines (GNU head):
head -n -10 file.txt - Suppress filename headers when checking multiple files:
head -q -n 3 *.txt - Combine with
tailto grab a specific middle chunk:head -n 50 file.txt | tail -n 10(lines 41-50) - Quickly check the shape of a large CSV before processing it:
head -n 1 data.csv
head -n 50 report.csv | tail -n 10
Common mistakes
- Assuming
headreads the whole file first — it doesn't need to for line-based output, but be aware-cbyte counts are exact regardless of line boundaries and can cut a line mid-character for multi-byte encodings. - Forgetting the default is 10 lines and being confused by unexpectedly short output.
- Using
head -n -Non macOS/BSDhead, which doesn't support the negative-count GNU extension the same way; checkman headon non-Linux systems. - Reaching for
headwhen you actually want the end of a file — that'stail's job.
Tips
head -n 1 fileis a quick way to grab just a CSV or TSV header row for scripting.- Pipe verbose command output through
headto avoid scrolling past what you need, e.g.find / -name "*.conf" | head -n 20. - Combine
headandtailto extract an arbitrary line range from a file without a full text editor.
Best practices
- Use
headinstead of opening large files in an editor just to check their format or first few records — it's far faster and doesn't risk accidentally modifying a huge file. - In scripts, prefer explicit
-ncounts over relying on the default 10, so behavior stays clear to future readers. - When working with binary data, use
-c(bytes) rather than-n(lines), since binary files often have no meaningful line structure.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Quickly checking the column headers of a large CSV export before writing a processing script.
- Sampling the first few log entries after a deployment to confirm logging is working:
head -n 20 app.log. - Verifying a downloaded file's type by inspecting its first bytes:
head -c 16 file.bin | xxd.
Common interview questions
- How would you print the first 20 lines of a file?
head -n 20 file.txt. - What's the difference between head and tail?
headprints the beginning of a file (default first 10 lines);tailprints the end (default last 10 lines). - How do you get just the header row of a CSV file?
head -n 1 file.csv, which prints only the first line.
Frequently Asked Questions
How do I show more or fewer than 10 lines?
Use -n followed by the count, e.g. head -n 20 file.txt for the first 20 lines, or head -n 5 file.txt for the first 5.
How do I print the first few bytes instead of lines?
Use head -c, e.g. head -c 100 file.bin prints the first 100 bytes, which is useful for peeking at binary files or checking file headers/magic numbers.
Can head show everything except the last few lines?
Yes, with GNU head: head -n -5 file.txt prints all lines except the last 5. This is a GNU extension and may not work on BSD/macOS head without adjustment.
How do I see the first lines of every file matching a pattern?
head -n 5 *.log prints the first 5 lines of each matching file, with a filename header before each one.
Cheat sheet
Download a quick-reference cheat sheet for head.