Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 cat or a pager.
  • Pipelines start here: head is 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: head for “what does this start with?”, tail for “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 20 prints the first 20 lines. You can also write head -20 file.txt — the shorthand works, but -n is 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 100 prints the first 100 bytes — useful for inspecting file headers, magic numbers, or binary signatures. (-b is 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, head prints 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 head works on anything. Note that head closes 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 like yes.

6. Combine with Sorting for “Top N” Checks

head -n 5 <(sort numbers.txt)
  • Process substitution lets head read from another command’s output as if it were a file — the first step toward the classic “top 10 biggest files” pipelines built with du and sort.

Common Pitfalls

  • head file isn’t the same as head -n 10 file on all systems. POSIX says the default is implementation-defined; GNU and BSD both use 10, but write -n 10 explicitly in scripts.
  • Binary output can confuse your terminal. head -c on a binary file prints garbage and may leave escape sequences active. Run reset afterwards if the prompt looks wrong.
  • head on multiple files needs the filename headers read carefully — the ==> name <== lines are not part of the file.
  • Counting lines with head in a loop is slow. Use wc -l for totals instead.

CommandDescription
tailThe last lines of a file
catPrint the whole file
lessScroll through a file page by page
grepFilter lines instead of slicing them
duSize of files head is sampling

head Cheatsheet

CommandWhat It Does
head fileFirst 10 lines
head -n 20 fileFirst 20 lines
head -c 100 fileFirst 100 bytes
head f1 f2First lines of several files, each labelled
head -n 5 *.confFirst 5 lines of every match
command | head -n 50First 50 lines of any output