Use tail command to print last part of files

by Daniel Pham
Published: Updated:
This entry is part 9 of 10 in the series Linux commands: Part 6 - Redirection

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.

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).

0 0 votes
Article Rating
Series Navigation«« Previous part: Use head command to print first part of filesNext part: Use tee to read from stdin and output to stdout and files »»

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.