What's the opposite of head? I want all but the first N lines of a file

Viewed 91210

Given a text file of unknown length, how can I read, for example all but the first 2 lines of the file? I know tail will give me the last N lines, but I don't know what N is ahead of time.

So for a file

AAAA
BBBB
CCCC
DDDD
EEEE

I want

CCCC
DDDD
EEEE

And for a file

AAAA
BBBB
CCCC

I'd get just

CCCC
9 Answers

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.

Related