head Command Guide for Viewing the First Lines of a File
head prints the beginning of a file — the fastest way to check what’s at the top of a log, config, or data file without reading all of it.
What is head, and Why Use It?
head reads the first N lines of a file (or several files) and prints them. The default is 10 lines, but you choose any count. It’s the mirror image of tail, which shows the end.
Where cat dumps everything, head gives you just the start — enough to identify a format, spot a header, or confirm a file is what you think it is.
Why Learn head?
- You need to identify a file’s format at a glance — CSV headers, script shebangs, config stanzas.
- Spot-checking large files without waiting for
cator a pager. - Pipelines start here:
headis often the first stage of examining a long stream. - It’s in scripts everywhere for extracting headers, sample rows, or line counts of the first chunk.
Rule of thumb:
headfor “what does this start with?”,tailfor “what happened last?”.
1. Print the First 10 Lines
head filename.txt
Example:
head data.csv
- No argument means 10 lines — the built-in default. Enough to see a CSV’s column headers or a script’s opening comment.
2. Choose How Many Lines
head -n 20 filename.txt
Example:
head -n 3 error.log
-n 20prints the first 20 lines. You can also writehead -20 file.txt— the shorthand works, but-nis clearer and more portable.
3. Count in Bytes or Blocks Instead of Lines
head -c 100 filename.txt
Example:
head -c 4 file.bin | xxd
-c 100prints the first 100 bytes — useful for inspecting file headers, magic numbers, or binary signatures. (-bis the older spelling of the same thing.)
4. Print the Start of Several Files
head file1.txt file2.txt file3.txt
Example:
head -n 5 *.conf
- With multiple files,
headprints a header naming each file before its lines. A quick way to survey the first lines of everything matching a glob.
5. Sample the Head of a Long Stream
cat huge.log | head -n 50
Example:
yes | head -n 5
- Piping into
headworks on anything. Note thatheadcloses the pipe early once it has its lines — the upstream command gets a SIGPIPE and stops, which is exactly what you want with infinite sources likeyes.
6. Combine with Sorting for “Top N” Checks
head -n 5 <(sort numbers.txt)
- Process substitution lets
headread from another command’s output as if it were a file — the first step toward the classic “top 10 biggest files” pipelines built withduandsort.
Common Pitfalls
head fileisn’t the same ashead -n 10 fileon all systems. POSIX says the default is implementation-defined; GNU and BSD both use 10, but write-n 10explicitly in scripts.- Binary output can confuse your terminal.
head -con a binary file prints garbage and may leave escape sequences active. Runresetafterwards if the prompt looks wrong. headon multiple files needs the filename headers read carefully — the==> name <==lines are not part of the file.- Counting lines with
headin a loop is slow. Usewc -lfor totals instead.
Related Commands
| Command | Description |
|---|---|
| tail | The last lines of a file |
| cat | Print the whole file |
| less | Scroll through a file page by page |
| grep | Filter lines instead of slicing them |
| du | Size of files head is sampling |
head Cheatsheet
| Command | What It Does |
|---|---|
head file | First 10 lines |
head -n 20 file | First 20 lines |
head -c 100 file | First 100 bytes |
head f1 f2 | First lines of several files, each labelled |
head -n 5 *.conf | First 5 lines of every match |
command | head -n 50 | First 50 lines of any output |