In this tutorial we will show you how to use the unix tail command. This command prints the last specified number of lines in a given file. The number of lines to be printed as the output is mentioned in the command. If you do not wish to specify the number then the command automatically displays the last ten lines of the file.
To understand the unix commands of tail follow the tutorial steps given below.
Step 1 – List contents of the directory
In the terminal, let’s start by listing the contents of the directory first.
Step 2 – Read the contents of the file
To understand the tail command, here we will first read the contents of this text file, using the cat command.
Step 3 – Enter command to see last entries on screen
Now we will use the tail command. The tail command is basically used to show the last few entries on the screen. You can define the number of entries to be displayed. If not specified, the last ten entries will be displayed on the screen.
Let’s start by typing “tail text.txt” and hitting enter.
Since we did not specify any amount over here, the last ten lines of the text file were displayed.
Step 4 – View last four lines of the file
Now let’s view the last four lines of this text file. For that, type the unix tail command
“tail -4 text.txt” and hit enter.
With that done, you will notice that only the last four lines of the file we displayed.
Step 5 – View data according to number of bytes
With the “-c” attribute, it will show you the data that is taking up the number of bytes mentioned in the command.
To make it clearer, let’s look at an example. Type in
“tail -c 13 text.txt” and hit enter.
With that done, you can see that the last 2 lines of the text file were displayed. If you look closely, the first letter in the first line is missing. That is because we have mentioned in the command, that only the data that is using up 13 bytes is to be displayed over here.
And that’s it. These are some of the ways you can use the unix commands of tail in Linux.