head - See the First Lines
Learn head for viewing the beginning of files. Quick previews and extracting top results.
head: Just Show Me the Beginning
You have a huge file. You don't want to see all 50,000 lines. You just want a preview:
head log.txt
First 10 lines. Quick peek. That's head.
Specifying How Many Lines
First 5 lines:
head -n 5 file.txt
First 20 lines:
head -n 20 file.txt
Or the shorthand:
head -5 file.txt
head -20 file.txt
In Pipelines
head is commonly used to limit output from other commands:
Top 10 most frequent items:
sort data.txt | uniq -c | sort -rn | head
First 5 matches:
grep "error" log.txt | head -5
Just the First Line
head -n 1 file.txt
Useful for grabbing headers or checking file format.
Multiple Files
head file1.txt file2.txt
Shows the first 10 lines of each, with headers separating them.
Quick Reference
| What you want | Command |
|---------------|---------|
| First 10 lines | head file |
| First N lines | head -n N file or head -N file |
| Limit pipe output | command \| head |
| Just first line | head -n 1 file |
See Also
- tail - the opposite (end of file)
- cat - whole file
Practice
In CTF challenges, head helps you preview large files quickly and grab just the top results from sorted data.
head is for peeking. When you don't need the whole thing, just ask for the top.