head

Show the first part of a file or stream; perfect for quick previews, log inspection, and pipeline testing.

Category: Text Processing coreutils files pipelines preview logs

Install

# Most systems: head is part of GNU coreutils (usually already installed)
head --version

# RHEL / Alma / Rocky
sudo dnf install coreutils

# Debian / Ubuntu
sudo apt install coreutils

# openSUSE
sudo zypper install coreutils

# Arch
sudo pacman -S coreutils

What it does

head prints the beginning of a file or standard input. By default it shows the first 10 lines, but it can also show a chosen number of lines or bytes. It is one of the quickest ways to preview data without opening an editor or paging through the entire file.

How it works (mechanical)

  • Opens one or more files, or reads from standard input if no file is given.
  • Counts lines by default, or bytes when given the -c option.
  • Stops reading as soon as the requested amount has been printed.
  • Works very well in pipelines because it exits early once it has enough input.
  • When multiple files are named, it prints headers unless suppressed by options or single-file usage.

Quick Start

# Show the first 10 lines of a file
head myfile.txt

# Show the first 20 lines
head -n 20 myfile.txt

# Preview command output
dmesg | head

10 Practical Examples

  1. Preview the top of a text file
    head notes.txt
  2. Show the first 25 lines
    head -n 25 /etc/services
  3. Show only the first 3 lines
    head -n 3 inventory.csv
  4. Preview the first 100 bytes of a file
    head -c 100 binary.dat
  5. Inspect the beginning of a log
    head -n 15 /var/log/messages
  6. Preview piped output
    ps aux | head
  7. Check a CSV header and first few rows
    head -n 6 sales.csv
  8. Show the first line only
    head -n 1 /etc/hostname
  9. Read from standard input interactively
    cat largefile.txt | head -n 12
  10. Preview several files at once
    head file1.txt file2.txt

Notes & Gotchas

  • Default is 10 lines. If you need a different amount, use -n explicitly.
  • Use -c carefully on text. Bytes are not the same as characters in all encodings.
  • Binary files may look messy. head will still print raw bytes if asked.
  • Multiple-file output includes headers. That is helpful for comparison, but can surprise scripts.
  • Early exit can affect pipelines. Some upstream commands may notice a broken pipe when head stops reading.

Historical Context

head is a long-standing Unix utility built for fast inspection of files and streams. It remains a core part of GNU coreutils and is still one of the most commonly used commands in shell work because it is simple, predictable, and pipeline-friendly.

Modern Equivalent

  • sed -n '1,10p' — more flexible when selecting exact line ranges
  • awk 'NR<=10' — useful when preview and processing happen together
  • bat --paging=never file | head — prettier file preview when bat is installed

Related Commands

  • tail — show the end of a file
  • less — scroll through a file interactively
  • sed — stream editor for line selection and editing
  • awk — pattern scanning and field processing
  • wc — count lines, words, and bytes
  • cat — print full file contents
  • tee — split output to screen and file
  • nl — number lines of a file