About the tail command
Everyone knows about the cat command alternative to the tail command , which is used to view the contents of files. But in some cases you don’t need to look at the whole file, sometimes it’s enough to look only at the end of the file. For example, when you want to see the contents of a log file, you do not need what it starts with, you will have enough of the latest error messages.
To do this, you can use the tail command, it allows you to output a specified number of lines from the end of the file, as well as display new lines online. This article will look at the tail Linux command.
Before we look at tail linux examples, let’s look at its syntax and options. And the syntax is very simple:
Code language: CSS (css)
# Usage tail [ OPTION ] ... [ FILE ] ....
By default, the utility outputs the last ten lines of the file, but its behavior can be configured using the options:
Code language: PHP (php)
# Options -c - output the specified number of bytes from the end of the file; -f - update information as new lines appear in the file; -n - print the specified number of lines from the end of the file; --pid - used with the -f option, allows you to terminate the utility when the specified process is completed; -q - do not display file names; --retry - try again to open a file if it is not available; -v - display detailed information about the file;
By default, the utility does not track name changes, but you can specify that you want to track the file by handle, for more details in the examples.
How to use the tail command
Now that you know the basic options, let’s look at how to work with the utility. The simplest example – we deduce the last ten lines of a file:
If you do not need 10 rows and need much more, you can increase this parameter with the -n option:
tail -n 10 /var/log/syslog
When you want to track the appearance of new lines in the file, add the -f option:
tail -f /var/log/syslog
You can open several files at once by simply listing them in the parameters:
tail /var/log/syslog /var/log/Xorg.0.log
You can use the -s option to set the refresh rate of the file. By default, the data is updated once a second, but you can configure, for example, a five-second update:
tail -f -s 5 /var/log/syslog
If you open multiple files, the file name will be displayed before the code section. If you want to remove this header, add the -q option:
tail -q var/log/syslog /var/log/Xorg.0.log
If you are not interested in the number of lines, but the number of bytes, you can specify them using the -c option:
tail -c 500 /var/log/syslog
For convenience, you can not select all the lines, but filter the ones you are interested in:
tail -f /var/log/syslog | grep err
This is especially useful when analyzing web server logs or finding errors in real time. If the file does not open, you can use the retry option to retry:
tail -f --retry /var/log/syslog | grep err
As I said at the beginning of the article, by default the -f or –follow option tracks the file by its name, but you can enable tracking mode by file descriptor, then even if the name changes, you will get all the information:
tail --follow=descriptor /var/log/syslog | grep err
The tail Linux command was discussed in this article. With its help it is very convenient to analyze logs of various services, and also to look for errors in them. I hope this information was helpful to you.