🏄‍♀️ Back to index learnings.bolmaster2.com

Use tail to remove lines from beginning of file

Published by bolmaster2

When you want to “cut”/remove the first line of a text content.

Create text with 5 lines:

1
2
3
4
5
6
$ echo -e "1\n2\n3\n4\n5"
1
2
3
4
5

The + character inverse the meaning of -n. It’s meaning: start on this line. This starts from the second line and prints the rest of the content.

1
2
3
4
5
echo -e "1\n2\n3\n4\n5" | tail -n +2
2
3
4
5

In contrast to the normal behavior of -n 1 which would print only the last line. Using tail like this is not the most obvious behavior as the name tail imply that you are working with the end of the file and not the beginning like the “sibling” command: head.

Works like this:

1
2
echo -e "1\n2\n3\n4\n5" | head -n 1
1