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
-coption. - 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
- Preview the top of a text file
head notes.txt
- Show the first 25 lines
head -n 25 /etc/services
- Show only the first 3 lines
head -n 3 inventory.csv
- Preview the first 100 bytes of a file
head -c 100 binary.dat
- Inspect the beginning of a log
head -n 15 /var/log/messages
- Preview piped output
ps aux | head
- Check a CSV header and first few rows
head -n 6 sales.csv
- Show the first line only
head -n 1 /etc/hostname
- Read from standard input interactively
cat largefile.txt | head -n 12
- Preview several files at once
head file1.txt file2.txt
Notes & Gotchas
- Default is 10 lines. If you need a different amount, use
-nexplicitly. - Use
-ccarefully on text. Bytes are not the same as characters in all encodings. - Binary files may look messy.
headwill 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
headstops 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 rangesawk 'NR<=10'— useful when preview and processing happen togetherbat --paging=never file | head— prettier file preview when bat is installed