Use tail command to print last part of files

Share this post to your friends !

This entry is part 9 of 10 in the series Linux commands: Part 6 - Redirection
Use tail command to print last part of files
5 (100%) 1 vote[s]

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

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
use-tail-command-to-print-last-part-of-files Use tail command to print last part of files
Use tail command to print last part of files.

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

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.

Continue reading the series«« Previous part: Use head command to print first part of filesNext part: Use tee to read from stdin and output to stdout and files »»
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.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.