Similar to cat command, Head and tail commands deal with displaying the content of a file. But they don’t display the entire content of it, instead show a certain number of lines of the file from head & tail part.
In this section, we will see some of the examples for head command and tail command individually.
Let’s start with head command,
By default, the head command will show top 10 lines of a file.
Show top lines of a file:
We have a file abclearn_lab4 and to look at its top 10 lines.
To see top 4 lines of a file,
Between -n and 4 you can keep the space or remove it. It works in both ways.
The tail command will show the number of lines from the bottom of the file.
Show bottom lines of a file:
Similar to head command it shows bottom 10 lines of a file by default.
If we want to see only last 3 lines of a file abclearn_lab4.txt
closing this file here.
To see continuous updates of a file:
The tail command has a wonderful feature to show, continuous updates of a file to the bottom of the page.
We can use “-f” option with tail command,
Assume we have a log file and want to observe the content updated dynamically.
Let’s take a file, which is having some 90 lines in it. And I want to display lines from line number 20 to 35. How can we do it?
We can use the combination of head and tail commands to display the output.