tail Command in Linux

tail Command in Linux to View End Part of a File

Dealing with files is one of the tasks of Linux system administrators. Acquiring more skills in file management increases efficiency and allows you to perform your tasks more quickly, efficiently, and effectively. Regularly monitoring changes made to important files, such as log files or configuration files, in real-time is helpful in troubleshooting. Therefore, Linux system administrators should learn quick methods to monitor file changes in real-time to manage and monitor essential files efficiently.

Various methods exist in Linux for viewing the content of files for monitoring, analysis, and troubleshooting purposes, and the command-line tool ‘tail’ is one of them. The tail command is an easy and fast way to observe changes and updates to files in real-time, allowing you to monitor the end portion of a file and be quickly informed of updates and additions of new data to the file. The tail command is handy when working with configuration and log files during troubleshooting and monitoring.

This article is a comprehensive guide on using the tail command in Linux distributions such as Ubuntu, Debian, CentOS, Fedora, and so on to view the end part of a file. It will enhance your skills in file management and interaction.

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 tail command displays the last few lines of a text file. This command is handy for momentary observation of the end of essential files, such as log files or other files that are constantly updated. To use the tail command for various scenarios, first, learn the basic syntax of the tail command in Linux distributions such as Ubuntu, Debian, CentOS, Fedora , which 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.

Note: The tail command is case-sensitive, so when typing standard input for tail processing, pay attention to the correct capitalization to avoid potential errors.

tail command options

Now that you understand the functionality and basic syntax of the tail command, it’s time to familiarize yourself with the most common options used with the tail command to customize its behavior. These options provide flexibility in configuring the tail command for specific needs. For example, you may want to print a particular number of lines/bytes or control real-time updates and other aspects of tail command behavior. Here are some common options of the tail command, along with explanations of their functionality:

  • -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 section of the article, we will explain some typical use cases for linux tail command by providing examples:

Checking the End of a File

If you want to quickly view the last 10 lines of a file, the tail command helps you. For this purpose, execute the tail command without any options. For example, to display the last 10 lines from the end of the file “test.txt,” 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

Using the ‘-c‘ option, you can configure the linux tail command based on the number of bytes to display end part of the file according to the specified byte count. This feature is valuable and efficient for ASCII data (where one byte equals one character) formatted into regular-sized records. For example, to view the last 54 bytes of the specified file, use 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>

For 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 inspecting the content of multiple files simultaneously using the tail command, displaying the header providing the file name assists you in examining the content of the files separately. Alternatively, if you wish to redirect the output of the tail command into a file, displaying the header with the file name is very useful for distinguishing the output of any file. Therefore, if you execute the tail command with the ‘-v‘ option, you can receive verbose output:

tail -v <File>

Or

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

For example:

tail -v test1.log test2.log test3.log

Viewing Updates in Real-time

In addition to outputting the end section of a file, the ability to display appended data is an admirable feature of the tail command. Therefore, if a process or command is currently running that redirects output to a file, the tail command helps you observe updates in real-time. For example:

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>

For example:

tail -n +12 test.log

Extracting part of a file and saving it to new file

In certain situations, you may want to extract a part of a file and save it in another file. In such cases, you need to redirect the output of the tail command to another file using the ‘>‘ option. For example, to save the last 15 lines of the “test.txt” file into a file named ” last-15-cases.txt,” run 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 functionality of the tail command extends when combined with other Linux commands, covering objectives such as sorting, file deletion, and other diverse goals. As a result, combining the tail command with other commands in Linux allows for more complex and customized operations. Using pipes and redirecting the output of the tail command to other tools enables the integration of the tail command with other commands. For example, to sort the output data of the tail command based on your needs, you can combine the tail command with the sort command. For instance, to sort the last 15 lines of the file “example.txt,” use 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 -10

To filter the output, you can combine the tail command with grep command, which we discussed how to use grep command before. For example, if you’re looking for lines containing the “error” word in a log file in Linux, you can use the following command:

tail -f /var/log/syslog | grep "error"

Conclusion

The tail command in linux is used for various scenarios, from displaying real-time updates of files to processing multiple files. This command-line tool offers flexible and powerful options catering to specific file management, monitoring, and analysis needs. In this article, you have learned how the linux tail command works and how to use it for different purposes. By mastering the use of the tail command, you have become more proficient in file management on Linux-based systems. We hope this article has been helpful to you in file management.

If you have any questions or encounter issues while using the tail command, feel free to share them with us in the comments section. To gain a comprehensive overview of tail options and usage, you can examine the manual page for ‘ tail by executing the man tail command.

Leave a Reply

Your email address will not be published. Required fields are marked.