WriteBash - 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
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
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.
linuxmint writebash.com # 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
$ tailf /var/log/messages
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.If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.
If you appreciate what we share in this blog, you can support us by:
- Stay connected to: Facebook | Twitter | Google Plus | YouTube
- Subscribe email to recieve new posts from us: Sign up now.
- Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
- Become a Supporter - Make a contribution via PayPal.
- Support us by purchasing Ribbon Lite Child theme being using on this website.
We are thankful for your support.