The head
function supports this with negative numbers.
head --help
-n, --lines=[-]K print the first K lines instead of the first 10;
with the leading '-', print all but the last
K lines of each file
For example, a positive number prints the first 2 lines.
head -n +2
AAAA
BBBB
A negative number prints all except for the first 2 lines.
head -n -2
CCCC
DDDD
EEEE
Note that unlike with tail
this does not require prior knowledge of the total number of lines in a file. It excludes the first 2 lines regardless of how many lines there are. Negative numbers for tail -n -2
can similarly be used to remove the last 2 lines of a file.
Note this uses GNU head version 8.22. This feature may not have been available at the original time of release. It is now available even if fairly old linux distros.