Master the Linux ‘tail’ Command: A Comprehensive Guide

Peter Hou
2 min readMay 9, 2023

--

Master the ‘tail’ command in Linux, a vital tool for viewing the end of text files. This guide covers the history, usage, parameters, and practical use-cases of ‘tail’.

Instructions

This article provides a comprehensive guide on how to use the ‘tail’ command in Linux. It is tailored towards beginners and includes a historical background, the reasoning behind its creation, usage instructions, common parameters, and practical use-cases.

History

The ‘tail’ command is part of the GNU core utilities package available on every Unix-like operating system. It was designed to output the last part of files.

When and why to use it

The ‘tail’ command is used primarily to view the end of text files. It is particularly useful for viewing the latest entries in log files, where new data is appended at the end.

How to use it

Simply type ‘tail’ followed by the name of the file you want to view. By default, ‘tail’ will display the last 10 lines of the file.

tail filename.txt

The commonly used parameters

  • -n This parameter is used to specify the number of lines from the end of the file that you want to display.
tail -n 20 filename.txt
  • -f This parameter allows you to follow the output of the file. As new lines are added to the file, 'tail' will update the output in real-time.
tail -f filename.txt

Other supported parameters

  • -c This parameter outputs the last 'n' bytes from a file instead of lines.
  • -q This parameter never outputs headers giving file names.
  • -v This parameter always outputs headers giving file names.

Most common use cases

  • Viewing the last few lines of a log file:
tail /var/log/syslog
  • Following the output of a log file in real-time:
tail -f /var/log/syslog

The tricky skills

The ‘tail’ command can be combined with other commands using pipes. For example, you can use ‘tail’ with ‘grep’ to view only certain lines from the end of a file.

tail filename.txt | grep "search term"

What needs to be noted

The ‘tail’ command is safe to use and will not modify your system. However, it’s important to remember that it will only display the end of files. If you need to view the beginning of a file, use the ‘head’ command.

Conclusion

The ‘tail’ command is an indispensable tool in Linux for managing and operating text files. Understanding how to use it can assist with system administration and troubleshooting, especially when dealing with log files.

--

--

Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.