...

Master Tail Command in Linux (or Unix) in 10 Minutes with Simple Use Cases

Rate this post

What is tail Command?

The tail command in Linux outputs a file’s end content(tail part). This command is very simple to use and is frequently used. E.g tail /var/log/syslog

Prerequisites

Learn the basics of Linux OS

Why do we need a tail command?

  • To see the end part of a file.
  • To live stream the log of a service through its log file. tail -f /var/log/nginx/access.log
  • To find the live pattern for a service by piping its log to grep. tail -f /var/log/nginx/access.log | grep 500

How to use the tail command in Linux (or Unix)

Syntax – tail option filename
Please go through the man page for details by executing man tail
Frequently used options with tail command

  • -f – To see live logs
  • -n – To Limit the number of lines in the output
tail command in Linux
tail command in Linux

Major Challenges in using the tail command?

Coming up with the correct tail command for the given problem

Let us explore this with an example.

Find the recent log in /var/log/nginx/access.log.
Solution – Recent logs mean – latest log – End logs of /var/log/nginx/access.log
And we know the end content of a file can be viewed using the tail command. So final command is tail /var/log/nginx/access.log

Find the latest 100 lines of log in /var/log/nginx/access.log.
Solution – latest logs mean – End logs of /var/log/nginx/access.log
And we know the end content of a file can be viewed using the tail command.
Next, we need the latest 100 lines -> Use -n 100 -> So the final command is tail -n 100 /var/log/nginx/access.log

Livestream logs of /var/log/nginx/access.log.
Solution – Livestream logs mean – latest log in a flowing manner – End logs of /var/log/nginx/access.log with -f (for live stream)
And we know the end content of a file can be viewed using the tail command. So final command is tail -f /var/log/nginx/access.log

Livestream 100 lines of log in /var/log/nginx/access.log.
Solution – Livestream logs mean – latest log in a flowing manner -> End logs of /var/log/nginx/access.log with -f (for live stream)
And we know the end content of a file can be viewed using the tail command and for live stream -> tail -f
Next, we need to limit to 100 lines -> Use -n 100 -> So the final command is tail -f -n 100 /var/log/nginx/access.log

With the above approach, you can solve any problem related to tail command in Linux.

Do some practice to grasp this command

FAQs on tail command in Linux (Unix)

Q1 – can we use the tail command in macOS as well?

A – Yes, it has the same behavior in macOS as well.

Conclusion

In this post, you learned about the tail command in Linux, its use case, and how to build a tail to see live patterns within a log file.

Related Posts:

Find Command in Linux

Spread the love

Leave a Comment

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.