head Cheatsheet
Quick reference for showing the first lines or bytes of files and limiting command output with head in Linux
The `head` command prints the beginning of files or standard input, with the first 10 lines shown by default. This cheatsheet covers line and byte counts, multiple files, output headers, pipelines, and GNU-specific options.
Basic Syntax
Core head command forms.
| Command | Description |
|---|---|
head FILE | Show the first 10 lines of a file |
head -n 20 FILE | Show the first 20 lines |
head -c 100 FILE | Show the first 100 bytes |
command | head -n 10 | Limit piped output to 10 lines |
head -- -notes.txt | Read a file whose name starts with - |
Select Lines
Control how many lines are printed.
| Command | Description |
|---|---|
head -n 5 /etc/passwd | Show the first 5 lines |
head -n 1 data.csv | Print the header row of a CSV file |
head -n 100 app.log | Preview the first 100 log entries |
head -n -5 FILE | Show all lines except the last 5 (GNU) |
head -n 2 FILE1 FILE2 | Show the first 2 lines of each file |
Select Bytes
Read a fixed number of bytes instead of lines.
| Command | Description |
|---|---|
head -c 100 FILE | Show the first 100 bytes |
head -c 2K FILE | Show the first 2048 bytes |
head -c 1MB FILE | Show the first 1,000,000 bytes |
head -c -512 FILE | Show all bytes except the last 512 (GNU) |
head -c 512 disk.img > header.bin | Save the first 512 bytes to a file |
Multiple Files and Headers
Control the file-name headers shown with multiple inputs.
| Command | Description |
|---|---|
head FILE1 FILE2 | Show 10 lines from each file with headers |
head -n 5 *.log | Show the first 5 lines of every matched log |
head -q FILE1 FILE2 | Suppress file-name headers |
head -v FILE | Always print a file-name header |
head -n 5 FILE1 - FILE2 | Read files and standard input in sequence |
Pipelines
Combine head with other text-processing commands.
| Command | Description |
|---|---|
ls -t | head -n 5 | Show the 5 most recently modified entries |
sort -nr scores.txt | head -n 10 | Show the 10 highest numeric values |
grep 'ERROR' app.log | head -n 20 | Show the first 20 matching errors |
tail -n +20 FILE | head -n 11 | Extract lines 20 through 30 |
head -n 20 FILE | wc -w | Count words in the first 20 lines |
Useful Options
Common GNU head flags and long forms.
| Option | Description |
|---|---|
-n NUM, --lines=NUM | Print the first NUM lines |
-c NUM, --bytes=NUM | Print the first NUM bytes |
-q, --quiet | Never print file-name headers |
-v, --verbose | Always print file-name headers |
-z, --zero-terminated | Read and write NUL-delimited items (GNU) |
--help | Show command help |
--version | Show the installed version |
Use head -n 5 instead of the obsolete head -5 syntax in scripts.
NUL-Delimited Input
Handle file names containing spaces, newlines, or other special characters.
| Command | Description |
|---|---|
find . -type f -print0 | head -z -n 5 | Select the first 5 NUL-delimited paths |
find . -type f -print0 | head -z -n 5 | xargs -0 -r ls -l | Pass the first 5 paths safely to ls |
printf 'one\0two\0' | head -z -n 1 | Print the first NUL-delimited item |
The -z option is specific to GNU head.
Troubleshooting
Quick checks for common head issues.
| Issue | Check |
|---|---|
| Only 10 lines are shown | This is the default; set the count with -n NUM |
| File-name headers appear | Multiple files trigger headers; add -q to suppress them |
| Binary output looks garbled | Redirect byte output to a file or inspect it with hexdump |
head -n -5 fails | Negative counts are a GNU feature and are not portable |
| Upstream command reports a broken pipe | head exits after collecting enough input; some producers report the closed pipe |
Related Guides
Use these guides and cheatsheets for complete text-processing workflows.
| Guide | Description |
|---|---|
| head Command in Linux | Full guide to lines, bytes, files, and pipelines |
| tail Cheatsheet | Read the end of files and follow logs |
| cat Cheatsheet | Display and combine file contents |
| sort Cheatsheet | Order lines before selecting top results |
| wc Cheatsheet | Count lines, words, and bytes |