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

This entry was posted in How to, File & Directory and tagged , , . Bookmark the permalink.

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

  1. Kjetil Skotheim says:

    seq 1 100
    Prints 1 to 100, one hundred lines

    seq 1 100 | perl -ne’splice@l,1,1 if @l>10; push@l,$_; print @l if eof’
    Prints 1 and 91-100

Comments are closed.