Hey, buddy. This time we will explain in detail the Linux tail command. Quite used by many sysadmin.
One of the most feared tools for newcomers is the Linux terminal. However, its use is decreasing every day thanks to the incredible improvements that the system has received. But in the administration of servers, there are no graphical interfaces to manage the system, for security reasons. So it’s up to you to familiarize yourself with the console and the commands.
And if we talk about commands there are many, but many syadmins use some more than the novice users. Tail is one of them and today we will help you to use it and tell you what it is used for.
The Tail command
Tail is a command to show the last lines of a text file. It is quite useful for exploring log files that generate services on Linux. Also, it is used to monitor changes that may have happened to this file.
:~$ tail [OPTION]… [FILE]…
Of course, this syntax is the basic form of usage. The behavior of the command is modifiable thanks to the options it has. To see them, use this command:
:~$ tail --help
However, I will explain with some examples of how easy and useful it is to use the Tail command.
Using the Linux tail command – examples
First, I created a text file containing several random names for this example. It is called example.txt
and has the following content:
Now let’s get started.
To display the last 10 lines of the file, just use the following command:
:~$ tail example.txt tom david joseph clancy richard robert henry harry paul
This is the most basic use of the command.
However, it is possible to change this behavior. For example, we can indicate how many lines to show. If we want to show the last 3 lines, just use it:
:~$ tail -n 3 example.txt harry paul
Simply replace the 3 with the number you want to display. Remember we’re always talking about the last few lines of the file.
On the other hand, we can show the lines that are after a specific line. For example, to show the lines after the 7th line:
:~$ tail +7 example.txt clancy richard robert henry harry paul
It’s super useful this option.
However, sysadmin uses this command to monitor log files. And it is possible to do it in real-time using the -f option. That is, the last lines of the file are displayed, but as it is modified, the output will be updated:
:~$ tail -f example.txt
Tail will monitor the file constantly, so press CTRL + C to exit.
Finally, you can display the command version as follows:
:~$ tail --version tail (GNU coreutils) 8.28 Copyright (C) 2017 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Paul Rubin, David MacKenzie, Ian Lance Taylor, and Jim Meyering.
And that is it.
Conclusion
The Tail command is a very useful terminal command. Widely used by many sysadmin for daily work it is also used for all those who wish to quickly view the contents of a file. Thanks to the examples, shown, you can see the potential of it and take advantage of it.
Please share this post and join our Telegram channel.