How To Use the ‘head’ Command in Linux to Preview Files

Updated

Linux terminal on an Ubuntu system

head -n 5 file prints the first 5 lines; head -c 50 file prints the first 50 bytes regardless of line breaks. Default with no flag is 10 lines. That’s the whole command — the rest of this page is the parts that actually trip people up, each verified on this machine (macOS 26.6, BSD head).

-c counts bytes, not characters, and doesn’t care about lines

head -c 5 sample.txt against a file containing abcdefghij\nklmnopqrst\n prints exactly abcde — no trailing newline, mid-word, because it stopped at byte 5. Use -c when you need a fixed-size peek (checking a file’s magic bytes, sampling a huge single-line JSON blob); use -n when the file is line-oriented and you want whole lines.

A file with no trailing newline doesn’t get one added

Text files are supposed to end with \n, but not all of them do — the last line of a truncated log or a hand-edited config often lacks it. Asking head for more lines than the file has doesn’t pad or error; it just prints what’s there, byte-for-byte:

$ printf 'line1\nline2\nline3' > nonl.txt   # note: no trailing \n
$ head -n 3 nonl.txt | xxd | tail -2
00000000: 6c69 6e65 310a 6c69 6e65 320a 6c69 6e65  line1.line2.line
00000010: 33                                       3

That last 33 is the bare byte for 3 — no 0a newline after it. If a script pipes head‘s output into something newline-sensitive, a source file missing its final newline will produce output missing its final newline too.

head on a binary file prints binary — use -c with a hex viewer

head has no file-type detection; run it on a binary and your terminal will try to render whatever bytes come out, which can garble the display or emit control sequences. Pair -c with xxd instead:

$ head -c 16 /bin/ls | xxd
00000000: cafe babe 0000 0002 0100 0007 0000 0003  ................

cafe babe is a Mach-O fat-binary magic number, not an ELF header — expected on macOS, and a reminder that “the first few bytes of a file” is itself a platform-specific fact.

yes | head -5 and the SIGPIPE surprise

yes writes y forever. Piped into head -5, it still terminates almost instantly — not because yes knows to stop, but because head exits after 5 lines, closes its end of the pipe, and the kernel sends yes a SIGPIPE the next time it tries to write. That’s a real, non-zero exit hiding under a script that looks like it succeeded:

$ bash -c 'set +o pipefail; yes | head -3 >/dev/null; echo "PIPESTATUS=${PIPESTATUS[@]}"'
PIPESTATUS=141 0

141 = 128 + 13 (SIGPIPE) — that’s yes‘s exit status, not head‘s. Under set -o pipefail the whole pipeline reports 141, which is correct behavior, not a bug, but it will fail a CI step that treats any non-zero pipeline exit as an error even though the output was exactly what you asked for.

head -n -5: works on GNU, refuses on BSD

GNU head accepts a negative count to mean “all but the last N lines.” This Mac’s head is BSD, and it does not:

$ head -n -5 sample.txt
head: illegal line count -- -5

Which head you have decides that last result, so head-command-to-preview-file.sh detects GNU or BSD at run time and checks the negative count against whichever it finds, along with the four checks above.

If you’re writing a script meant to run on both, don’t rely on negative -n — use sed -n "1,$(($(wc -l < file)-5))p" or check for GNU head with head --version 2>/dev/null | grep -q GNU first. For continuous log-watching instead of a one-time preview, see tail -F for following file changes.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.