G2Labs Grzegorz Grzęda
Linux tail command
June 10, 2024
Mastering the Linux ‘tail’ Command: A Deep Dive
The tail
command is an invaluable tool for any Linux user. With just a few keystrokes, you can access the last few lines of a file in real-time, monitor log files, or even follow changes in a continuously updated file. Despite its simplicity, the tail
command is highly versatile and can be a major time-saver in a wide range of scenarios.
In this post, we will explore the various options and use cases for the tail
command with extensive examples. So, whether you are a beginner or an experienced Linux user, get ready to take your skills up a notch!
Basic Usage
Let’s start with the most straightforward usage of the tail
command. Open your terminal and try the following command:
|
|
By default, tail
outputs the last 10 lines of a file. Replace file.txt
with the path of the file you want to examine. The output will display the last 10 lines of the file on your screen.
Navigating through Lines
Changing the Number of Lines
Often, you may want to adjust the number of lines displayed. To specify the number of lines explicitly, use the -n
option followed by the desired number. For example, to see the last 5 lines of a log file, run:
|
|
‘Follow Mode’
One of the most powerful features of the tail
command is the ability to use it in follow mode. By appending the -f
option, tail
will keep displaying newly added lines to a file in real-time. This is particularly useful when monitoring log files or tracking the progress of a continuously updated process.
|
|
Now, every new line added to app.log
will instantly appear on your screen, giving you live updates of the file’s content. Press Ctrl + C
to exit follow mode.
Combining Files
The tail
command also allows you to combine multiple files into one continuous stream of output. To achieve this, use the -q
option to suppress headers provided with each file and -s
to insert separators between files.
|
|
With this command, the contents of file1.txt
, file2.txt
, and file3.txt
will be concatenated, and separators will be inserted between each file. Feel free to modify the separator string to match your needs.
Output Control
Displaying Line Numbers
If you need to keep track of line numbers while viewing the output, you can use the -n
option with the cat
command in combination with tail
.
|
|
This command will print the contents of file.txt
with line numbers, and tail
will then output the last 10 lines, preserving the line numbers. Adjust the tail
options to modify the number of lines as required.
Multiple Outputs
In some cases, you may want to display the output to both the terminal and a new file simultaneously. By using output redirection, you can achieve this easily.
|
|
This command continuously displays new lines from app.log
on the screen while writing them to new.log
concurrently. Now, you have a copy of live updates in a separate file for future reference.
Conclusion
The tail
command is an indispensable tool for navigating and monitoring files in Linux. By mastering its various options and use cases, you can streamline your workflow and save precious time.
In this post, we went through the basics of using tail
, including adjusting the number of lines, utilizing follow mode, and combining files. We also explored how to display line numbers and create multiple outputs.
Now, it’s your turn to apply this knowledge and become a productivity ninja in your Linux environment. Happy tailing!