The head command prints the beginning of files or input streams.
It is commonly used to preview data, inspect headers, and limit output in scripts.
Overview
Use head when you need to:
- Preview the first lines of large files
- Inspect file headers (CSV, logs, configs)
- Limit pipeline output for quick checks
- Read the first N bytes of binary/text streams
- Reduce noisy output during debugging
Syntax
head [options] [file...]
Common forms:
# Default: first 10 lines
head app.log
# First 20 lines
head -n 20 app.log
# First 100 bytes
head -c 100 payload.bin
If no files are provided, head reads from standard input.
Default Behavior
- Without options,
headprints the first 10 lines. - With multiple files, file headers are printed before each section.
headis read-only and does not modify input files.
Common Options
| Option | Purpose | Example |
|---|---|---|
-n N |
Show first N lines | head -n 30 app.log |
-c N |
Show first N bytes | head -c 256 data.bin |
-q |
Never print file name headers | head -q file1 file2 |
-v |
Always print file name headers | head -v file1 file2 |
GNU head also supports suffixes with -c such as K, M, and G.
Examples
# Preview first 20 lines of a log
head -n 20 app.log
# Show first line (useful for CSV headers)
head -n 1 users.csv
# Show first 500 bytes of a file
head -c 500 archive.dat
# Preview first 3 lines from each of multiple files
head -n 3 server1.log server2.log
# Suppress per-file headers when reading multiple files
head -n 2 -q a.txt b.txt c.txt
# Force per-file headers when reading multiple files
head -n 2 -v a.txt b.txt c.txt
# Limit command output in a pipeline
journalctl -u nginx | head -n 50
# Inspect first few paths from find output
find . -type f | head -n 10
Working with Bytes vs Lines
-nis line-oriented and respects line boundaries.-cis byte-oriented and may cut in the middle of a line or multibyte character.
Use -n for human-readable text previews.
Use -c for protocol headers, binary checks, or fixed-size reads.
Integration Patterns
Quick log inspection:
head -n 40 /var/log/syslog
Preview generated output from scripts:
./generate-report.sh | head -n 25
Validate sorted output quickly:
sort big-list.txt | head -n 10
Safe Usage Guidelines
- Use
headbefore full processing to validate file format and structure. - Combine with
-n 1to safely inspect headers in CSV or TSV files. - Use
-qin scripts when predictable output formatting is required. - Prefer
-nfor text and-cfor byte-accurate inspection.
Performance Tips
headis efficient for large files because it stops reading after requested output.- Use
headearly in pipelines during debugging to reduce processing time. - For bottom-of-file inspection, use
tailinstead of reading full file then filtering.
Troubleshooting
Output includes ==> file <== headers unexpectedly
Cause: Multiple files were passed, so head shows per-file labels.
Fix:
head -q -n 5 file1 file2
head -c output looks truncated or garbled
Cause: Byte truncation may split multibyte text characters.
Fix:
- Use
-nfor text data - Keep
-cfor raw byte inspection use cases
Need first N lines but from piped command
Fix:
some_command | head -n 20
Notes
head and tail are complementary tools for quick top/bottom inspection of data.
For command details and implementation-specific behavior, run:
man head
head --help