head — Show the First Lines of Files
Practical guide to head: show the first lines or bytes of a file, check headers and limit output in pipelines.
head shows you the beginning of a file – by default the first 10 lines. With it you take a quick look inside large files, check the header row of a CSV or limit output in pipelines to the first hits. Use -n to set the line count and -c to work byte by byte. Handy: GNU head also understands negative counts such as -n -3 (everything but the last three lines). This guide walks you through the common options and their typical pipeline partners.
Basic Usage
head <file> — Show the first 10 lines of a file (default).
head access.loghead -n <count> <file> — Show the first N lines of a file.
head -n 20 access.loghead -<count> <file> — Shorthand for -n. Show the first N lines.
head -5 data.csvhead -n -<count> <file> — Show all lines except the last N lines.
head -n -3 data.txtByte Mode
head -c <bytes> <file> — Show the first N bytes of a file instead of lines.
head -c 100 binary.dathead -c <size>K <file> — Show the first N kilobytes. Supports K, M, G suffixes.
head -c 4K large-file.binhead -c -<bytes> <file> — Show all bytes except the last N bytes.
head -c -10 data.binMultiple Files
head <file1> <file2> — Show the first 10 lines of each file with filename headers.
head error.log access.loghead -n <count> <file1> <file2> — Show the first N lines of multiple files.
head -n 5 *.loghead -q <file1> <file2> — Quiet mode. Suppress filename headers when showing multiple files.
head -q -n 1 *.csvhead -v <file> — Verbose mode. Always show filename header, even for a single file.
head -v -n 5 data.txtPipelines
<command> | head — Show only the first 10 lines of command output.
ps aux | head<command> | head -n <count> — Limit command output to the first N lines.
find / -name '*.conf' 2>/dev/null | head -n 20<command> | head -n 1 — Get only the first line of output (e.g. header row or first result).
docker ps | head -n 1sort <file> | head -n <count> — Show the first N items after sorting (e.g. smallest values).
sort -n scores.txt | head -n 5<command> | head -c <bytes> — Limit output to a specific byte count from a pipeline.
cat /dev/urandom | head -c 32 | base64Common Patterns
head -n 1 <file> — Show only the header line of a CSV or TSV file.
head -n 1 data.csvhead -n 1 <file> && tail -n +2 <file> | sort — Print header line, then sort the remaining data lines.
head -n 1 data.csv && tail -n +2 data.csv | sort -t',' -k2head -c 4 <file> | xxd — Check the magic bytes (file signature) of a binary file.
head -c 4 image.png | xxdfor f in *.log; do echo "=== $f ==="; head -n 3 "$f"; done — Preview the first 3 lines of each log file with custom headers.
for f in *.log; do echo "=== $f ==="; head -n 3 "$f"; done Conclusion
head is the natural counterpart to tail and one of the fastest ways to look at a file without loading it whole – ideal for huge files too, since head simply stops reading once it has enough lines. Note that the negative counts (head -n -3, head -c -10) are a GNU extension and do not work on BSD or macOS. To cut out a middle range of lines, combine head with tail, for example head -n 20 | tail -n 5. With -c head reads binary data too – but take care mixing -n and -c, as the two are mutually exclusive.
Further Reading
- GNU Coreutils manual: head – complete reference for every option
- man7.org: head(1) – the Linux manual page