brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: tail -n +2 prints a file from its second line, so the header is gone

Tail
~/tips/tip-linux-tail-n-plus-skip-header

tail -n +2 export.csv

Prints a file starting at a chosen line, for example to skip a CSV header.

Most people know tail -n 5 for the last five lines. Fewer know that a plus sign flips the meaning completely: tail -n +5 starts printing at line 5 and keeps going to the end of the file.

That makes it the cleanest way I know to drop a header line before you process the rest:

tail -n +2 export.csv

The man page describes it as skipping NUM minus one lines at the start, so +2 skips exactly one line and +1 prints the whole file. That off-by-one feels odd at first, until you read the number as “start at line N”.

Where this shines is in front of other tools that would otherwise treat the header as data. Sorting a CSV on its third column without the column names landing somewhere in the middle:

tail -n +2 export.csv | sort -t, -k3,3n

Or counting the rows of a file without counting the header, with tail -n +2 export.csv | wc -l. The same works on command output that starts with a title line, like df -h or ps aux.

GNU head has the opposite trick, which pairs nicely with this one. A minus sign in head -n -1 prints everything except the last line, handy for a trailing total or a footer. Combine both and you keep only the body of a report.

The caveat: tail doesn’t know anything about headers. It just skips lines, so make sure the file really has exactly one header line before you put this in a script. Run it on a file without one and you silently lose your first record. Also, the negative head form is a GNU extension, so it won’t work everywhere tail’s plus sign does.

Further reading: tail(1) manual page, head(1) manual page

Going deeper: Moving on from hdtv: how to export your TV show list to Trakt.tv

More Tail tips tail -c 2000 export.json tail -f --pid=1234 build.log tail -f /var/log/nginx/access.log /var/log/nginx/error.log tail -F /var/log/nginx/access.log All Tail tips (5) All topics

Link