View the End of a File in Linux Using the “tail” Command

Published December 21, 2023

View the End of a File in Linux Using the “tail” Command.

Linux 
tail command

Explore More ; Rocky Linux vs CentOS

Linux system administrators often find themselves dealing with files as part of their daily tasks. By acquiring additional skills in file management, they can greatly enhance their efficiency and perform tasks more swiftly and effectively. One crucial aspect of file management is regularly monitoring changes made to important files, such as log files or configuration files, in real-time.

This proactive approach can be immensely helpful in troubleshooting and resolving issues promptly.

Therefore, it is imperative for Linux system administrators to learn quick and efficient methods for monitoring file changes in real-time, enabling them to effectively manage and monitor essential files.

Prerequisites to use Linux tail command

  • Using Linux system or buy Linux VPS.
  • Access to the Terminal (using the shortcut keys Ctrl+Alt+T).

tail Syntax in Linux

The exquisite tail command unveils the final few lines of a splendid text file. This command is a delightful tool for momentarily observing the captivating conclusion of vital files, such as enchanting log files or other mesmerizing files that are incessantly updated. To indulge in the versatility of the tail command across diverse scenarios, one must first acquaint themselves with the elegant syntax of this command in esteemed Linux distributions like Ubuntu, Debian, CentOS, and Fedora. The distinguished syntax is as follows:

tail <options> <file>

  • options: To extend the functionality of the tail command, you can use the options supported by the tail in this field.
  • file: Enter the name of the file or files whose end part you intend to monitor.

tail command options

Gain a deeper understanding of the tail command by exploring its various options that allow you to customize its behavior. These options provide flexibility in tailoring the command to suit your specific needs. Whether you need to print a specific number of lines or bytes, or control real-time updates, the tail command has got you covered. Familiarize yourself with these common options to unlock the full potential of the tail command.

  • “-c N” or “–bytes=N“: Displays the last N bytes of the file.
  • “-n N” or “–lines=N“: Outputs the last N lines of the file instead of the default number of lines (usually the last 10 lines of the file).
  • “-f” or “–follow“: Displays appended data as the file grows. This option is useful for real-time monitoring of important files, such as log files. It even shows updated information after the original file is deleted or replaced with a new file with the same name.
  • “-q” or “–quiet,” “–silent“: Prevents the display of headers that provide the file name and hides the file name.
  • “–pid=PID“: when combined with the “-f” option, terminates after process ID, PID, dies.
  • “-v” or “–verbose“: Outputs headers that provide the file name.
  • “–retry“: It Attempts to open a file that is inaccessible when starting the tail command or later.
  • “-version“: Displays information about the tail command version.
  • “–help“: Prints more detailed information about the tail command.

How to Use tail Command in Linux?

In this part of the article, we will delve into various practical scenarios where the linux tail command proves to be incredibly useful.

Let’s explore these use cases through illustrative examples:

Checking the End of a File

Discover the convenience of the tail command when you need to swiftly access the last 10 lines of a file. By simply executing the tail command without any additional options, you can effortlessly achieve this task. For instance, if you wish to view the final 10 lines of the file “test.txt,” just run the following command:

tail test.txt

The tail command, without any options, prints the 10 last lines of the content of a file by default.

Monitoring the last N lines of a file

When dealing with large files, using the tail command in linux ubuntu to display a specific number of lines from the end of the file instead of reading the entire content is more efficient. For example, if you want to examine the last 100 lines of the “largefile.log” file, run the following command:

tail -n <Number-of-last-N-Lines> <File>

For example:

tail -n 100 largefile.log    # Display the last 100 lines of a large file

Using the ‘-n‘ option allows you to specify a particular number of lines from the end of the file to view and also change the default behavior of the tail command in displaying the last part of a file. You can use the previous command to continuously monitor the changes when a script or process modifies a file on the Linux system.

Checking the last N bytes of a file

Enhance your Linux tail command experience with the powerful ‘-c’ option. By specifying the number of bytes, you can effortlessly configure it to display the desired end part of any file. This remarkable feature proves to be incredibly valuable and efficient, especially when dealing with ASCII data that is neatly organized into regular-sized records.

For instance, if you wish to view the last 54 bytes of a specific file, simply execute the following command:

tail -c <number_of_bytes> <file_name> tail -c 54 testfile.log 

Viewing an end part of files using the tail for multiple files

To apply the tail command function to multiple files and obtain output from the end section of several files simultaneously, you can use the following syntax:

tail <options> <File-1> <File-2> <File-n>

For example, if you want to check the last 30 lines of files “test1.log” and “test2.log,” enter the following command:

tail -n 30  test1.log  test2.log

Merging content of multiple files using tail command

Since the tail command, by default, displays the header with the name of each file when showing the end section of a file, you can use the following command to enable quiet mode and prevent the display of file name headers:

tail –q <File-1> <File-2> <File-n>

example:

tail -q test1.log  test2.log

As a result, the content of the files is merged by executing the previous command, and the file name headers are removed from the output.

Separating the output of multiple files via file names

When you use the tail command to inspect the content of multiple files at once, it’s helpful to see the file name in the header. This allows you to examine the content of each file separately. If you want to redirect the output of the tail command to a file, displaying the header with the file name becomes even more useful. By using the ‘-v’ option with the tail command, you can receive a detailed and informative output.

tail -v <File>

Viewing Updates in Real-time

The tail command is not only capable of displaying the end section of a file, but it also has the remarkable ability to show appended data. This makes it an invaluable tool for monitoring real-time updates when a process or command is redirecting output to a file.

For instance:

some_command > output.log &   # Run a command in the background and redirect its output to a file tail -f output.log           # View updates in real-time

This feature of the tail command in linux is helpful for monitoring and tracking log files in real-time because it allows you to check the new status of log files after adding new input or changes. It is worth mentioning that the log file must be readable for the tail command. For continuous monitoring of log files in real-time using the tail command, use the following command in Linux:

tail -f /var/log/syslog

Checking a specific range of file content

The tail command-line tool allows you to specify a range in the content of a file to display. By specifying the desired line number in the file’s content, you can customize the ubuntu tail command to show content from the specified line number to the end of the file. For example, if you want the tail command to print content from the 12th line to the end of the file “test.log,” execute the following command:

tail -n +<Line-number> <File>

example:

tail -n +12 test.log

Extracting part of a file and saving it to new file

If you ever find yourself in a situation where you need to extract a specific portion of a file and save it separately, there’s a handy trick you can use. By redirecting the output of the tail command to another file using the ‘>’, you can achieve this effortlessly. Let’s say you want to save the last 15 lines of a file called “test.txt” into a new file named “last-15-cases.txt.” Simply execute the following command:

tail -n 15 test.txt > last-15-cases.txt

To ensure the successful redirection of the last 15 lines of the content from the “test.txt” file into a new file, you can view the content of the file using the cat command

Combining the tail command with other commands

The tail command in Linux becomes even more powerful when combined with other commands, offering a wide range of functionalities such as sorting, file deletion, and achieving various objectives. By integrating the tail command with other tools through pipes and output redirection, you can perform complex and customized operations. For instance, if you want to sort the output data of the tail command according to your requirements, you can easily combine it with the sort command.

To illustrate, if you wish to sort the last 15 lines of the file “example.txt,” simply execute the following command:

tail -n 15 example.txt | sort – R

As a result, the last 15 lines of the “example.txt” file were used as standard input for the sort command to rearrange this output in random order (due to the use of the ‘-R‘ option with the sort command).

If you want the output of the tail command linux to be sorted in ascending order based on months, you can use the ‘-M‘ option with the sort command:

tail -n [number_of_lines] [file_name] | sort -M

Combining the head and tail Commands have various uses for file analysis and monitoring. For example, you can use the head command to display the beginning part of file content and check the last part of the head command’s output in combination with the tail. As a result, this combination allows you to inspect a specific range of file content without the need to read the entire content.

For instance, if you want to extract the first 30 lines of a file and then view the last 10 lines of that extracted part from the beginning of the “example.txt” file, execute the following command:

head -n 30 example.txt| tail -1

Explore more; Common Linux Vulnerabilities in Home Environments
Stay Connected on Meta ,X, Instagram .