Print N first lines of multiple files on MacOS, OpenBSD or FreeBSD

Tomasz Marciniak

April 21, 2022

Summary: head command prints first lines of given file. On GNU/Linux and NetBSD -q option suppreses printing of headers with filenames, when multiple of them are passed as arguments. OpenBSD, FreeBSD and MacOS don’t have this switch, so how do you achieve the same result with basesystem tools only?

AWK

The old but powerful AWK (the non-GNU version) included in BSD and MacOS can do this. Let’s say we want first two lines of every supplied file:

awk 'NR == 3 { NR=0; nextfile } NR <= 2' file1 file2