tail
The tail command is a go-to tool for viewing the end of a file. It’s particularly useful for checking recent entries in log files or quickly glancing at the latest changes in a document.
This command will display the last 10 lines of the file_name
tail file_name
Sometimes, you might need to view more or fewer lines than the default 10. The -n option (short for “number”) allows you to specify exactly how many lines you want to see.
This command will display the last 5 lines of the file_name
tail -n 5 file_name
Viewing Content from a Specific Line
While tail is typically used to view the end of a file, you can also use it to display content starting from a specific line number. This is done by using a plus sign (+) before the line number.
Let’s view the content of our log file starting from the 50th line. This command will display all lines from the 50th line to the end of the file.
tail -n +50 file_name
Monitoring File Changes in Real-Time
One of the most powerful features of tail is its ability to monitor files in real-time. This is particularly useful for watching log files as they’re being written to. We use the -f option (which stands for “follow”) to achieve this.
Let’s monitor our log file in real-time. After running this command, you’ll see the last 10 lines of the file, followed by any new lines that are added to the file in real-time.
tail -f file_name
Combining Options for Advanced Usage
The tail command becomes even more powerful when you combine different options. Let’s combine the -n option with the -f option to monitor the last 3 lines of our log file in real-time. his command will display the last 3 lines of the file and then continue to show any new lines added to the file. This can be useful when you want to focus on the very latest entries in a rapidly changing log file.
tail -n 3 -f file_name
Summary
In this tutorial, we explored the versatile tail command in Linux, focusing on its application in monitoring log files. We learned how to:
- View the last lines of a file using the basic tail command
- Customize the number of lines displayed using the -n option
- View content starting from a specific line using tail -n +N
- Monitor file changes in real-time with the -f option
- Combine options for more advanced usage
These skills are fundamental for system administration, debugging, and monitoring tasks. As you become more comfortable with tail