In this article, we will learn about tail
commands. We use tail command to print last part of files.
In the previous article, you read about the head command. That command prints the first part of the file, and this command prints the end of the file.
Tail command syntax
Table of Contents
The tail
command is a brother command with the head
command, so the command syntax is quite similar.
Use this command without options.
$ tail [file]
Use this command with options.
$ tail [options] [file]
And use it in pipeline.
$ command-1 | ... | tail [options] | ... | command-n
Example use tail command to print last part of files
We will reuse the file ls-bin.txt
as in the article about the head
command.
First, type the tail
command without any options.
$ tail ls-bin.txt
Next, type use tail
with the -n
option and print the first 5 lines.
$ tail -n 5 ls-bin.txt
zforce
zgrep
zless
zmore
znew
Similarly, you can type the following command to use the this command in the pipeline.
$ ls /bin | tail -n 5
Recommended Reading: What is pipeline and how to use it
Use tail command to view the realtime log
A rather special option of the tail
command allows us to use it to view realtime logs.
That is the -f
– following option. This option will continue to print the new lines that appear even when the file is recording data realtime.
You imagine, your system is having an error. You need to see the system log in the current realtime state.
The tail command with the -f
option will do that. A log file that most people see when troubleshooting, /var/log/syslog
(on Ubuntu) or /var/log/messages
(on CentOS).
There are 2 ways to type the command as follows.
$ tail -f /var/log/messages
Or:
$ tailf /var/log/messages
Conclusion
This command is very useful when troubleshooting, you will need it to know what error your system is having. Use the -f
option and you will know how useful it is.
One more thing, you can combine the head and tail commands into the same pipeline. This will give you a limited area of data. For example, you just want to see the first line in the last 5 lines of a file.
(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).