How to display the first line and the last few lines of a file in Linux

In Linux system, when we deal with files, sometimes we have to print the first line and the last few lines of the file.

At this time, we will use the Linux tail command and the head command or awk command.

Let’s see how to use Linux command.

Print the first line and the last 10 lines of the file.

Using the head and tail commands

➜ head -1 test.log; tail -10 test.log

Print the first line of the file and the last 10 to 15 lines of the file

using the awk command

➜ awk '{if(NR == 1 || (NR >= 10 && NR <= 15)) {print $0}}' test.log

Add a Comment

Your email address will not be published. Required fields are marked *