Skip to main content

head Cheatsheet

By Dejan Panovski Updated on Download PDF

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.

CommandDescription
head FILEShow the first 10 lines of a file
head -n 20 FILEShow the first 20 lines
head -c 100 FILEShow the first 100 bytes
command | head -n 10Limit piped output to 10 lines
head -- -notes.txtRead a file whose name starts with -

Select Lines

Control how many lines are printed.

CommandDescription
head -n 5 /etc/passwdShow the first 5 lines
head -n 1 data.csvPrint the header row of a CSV file
head -n 100 app.logPreview the first 100 log entries
head -n -5 FILEShow all lines except the last 5 (GNU)
head -n 2 FILE1 FILE2Show the first 2 lines of each file

Select Bytes

Read a fixed number of bytes instead of lines.

CommandDescription
head -c 100 FILEShow the first 100 bytes
head -c 2K FILEShow the first 2048 bytes
head -c 1MB FILEShow the first 1,000,000 bytes
head -c -512 FILEShow all bytes except the last 512 (GNU)
head -c 512 disk.img > header.binSave the first 512 bytes to a file

Multiple Files and Headers

Control the file-name headers shown with multiple inputs.

CommandDescription
head FILE1 FILE2Show 10 lines from each file with headers
head -n 5 *.logShow the first 5 lines of every matched log
head -q FILE1 FILE2Suppress file-name headers
head -v FILEAlways print a file-name header
head -n 5 FILE1 - FILE2Read files and standard input in sequence

Pipelines

Combine head with other text-processing commands.

CommandDescription
ls -t | head -n 5Show the 5 most recently modified entries
sort -nr scores.txt | head -n 10Show the 10 highest numeric values
grep 'ERROR' app.log | head -n 20Show the first 20 matching errors
tail -n +20 FILE | head -n 11Extract lines 20 through 30
head -n 20 FILE | wc -wCount words in the first 20 lines

Useful Options

Common GNU head flags and long forms.

OptionDescription
-n NUM, --lines=NUMPrint the first NUM lines
-c NUM, --bytes=NUMPrint the first NUM bytes
-q, --quietNever print file-name headers
-v, --verboseAlways print file-name headers
-z, --zero-terminatedRead and write NUL-delimited items (GNU)
--helpShow command help
--versionShow 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.

CommandDescription
find . -type f -print0 | head -z -n 5Select the first 5 NUL-delimited paths
find . -type f -print0 | head -z -n 5 | xargs -0 -r ls -lPass the first 5 paths safely to ls
printf 'one\0two\0' | head -z -n 1Print the first NUL-delimited item

The -z option is specific to GNU head.

Troubleshooting

Quick checks for common head issues.

IssueCheck
Only 10 lines are shownThis is the default; set the count with -n NUM
File-name headers appearMultiple files trigger headers; add -q to suppress them
Binary output looks garbledRedirect byte output to a file or inspect it with hexdump
head -n -5 failsNegative counts are a GNU feature and are not portable
Upstream command reports a broken pipehead exits after collecting enough input; some producers report the closed pipe

Use these guides and cheatsheets for complete text-processing workflows.

GuideDescription
head Command in LinuxFull guide to lines, bytes, files, and pipelines
tail CheatsheetRead the end of files and follow logs
cat CheatsheetDisplay and combine file contents
sort CheatsheetOrder lines before selecting top results
wc CheatsheetCount lines, words, and bytes