Linux Tutorial
Linux File/Directory Management
Linux Packaging And Compression
Vim Text Editor
Linux Text Processing
Linux Software Installation
Linux User/User Group Management
Linux Permission Management
Linux Filesystem Management
Linux Advanced Filesystem Management
Linux System Management
Linux Backup and Recovery
Linux System Service Management
Linux System Log Management
Linux Boot Management
LAMP/LNMP Environment
SELinux Management
In this tutorial, we will explore the tail
command in Linux. The tail
command is used to display the last part (the tail end) of a file. It is particularly useful for monitoring log files, as it can help you keep track of recent events in real-time.
Basic Usage of the tail Command
The basic syntax for the tail
command is as follows:
tail [OPTIONS] [FILE]
By default, the tail
command displays the last 10 lines of a file. To display the last 10 lines of a file named example.txt
, simply run:
tail example.txt
Displaying a Specific Number of Lines
To display a specific number of lines from the end of a file, use the -n
option followed by the number of lines you want to display. For example, to display the last 5 lines of example.txt
, run:
tail -n 5 example.txt
Monitoring a File in Real-Time
One of the most powerful features of the tail
command is its ability to monitor a file in real-time, displaying new lines as they are added. This is useful for keeping track of log files or other files that are updated frequently.
To monitor a file in real-time, use the -f
option, as shown below:
tail -f example.txt
The tail
command will keep running, and new lines will be displayed as they are added to the file. To exit the real-time monitoring, press Ctrl+C
.
Displaying Multiple Files
The tail
command also supports displaying the last part of multiple files at once. Simply provide a list of file names as arguments, like this:
tail file1.txt file2.txt file3.txt
The output will show the last 10 lines of each file, with a header indicating the file name.
Displaying Bytes Instead of Lines
If you want to display a specific number of bytes from the end of a file instead of lines, use the -c
option followed by the number of bytes you want to display. For example, to display the last 100 bytes of example.txt
, run:
tail -c 100 example.txt
Summary
The tail
command in Linux is a powerful tool for displaying the last part of a file. It is especially useful for monitoring log files in real-time. By using various options, such as -n
, -f
, -c
, and more, you can customize the tail
command to display the desired number of lines or bytes, monitor multiple files, or follow file updates in real-time.
Displaying the last lines of a file with tail
:
tail
command followed by the file name.# Example: Displaying the last lines of a file tail filename
Following and monitoring log files with tail
:
-f
option with tail
.# Example: Following and monitoring log file tail -f /var/log/syslog
Customizing output format with tail
in Linux:
tail
command allows customization of the output format, such as showing line numbers or adding headers.# Example: Customizing output format tail -n 10 -v filename
Viewing a specific number of lines with tail
:
-n
option followed by the desired line count.# Example: Viewing a specific number of lines tail -n 20 filename
Using tail
for real-time file updates in Linux:
-f
option in tail
allows continuous monitoring and displays updates in real-time as new lines are added to a file.# Example: Real-time updates with tail tail -f /var/log/nginx/access.log
Scrolling through large files with tail
:
-n
option to set the number of lines to display.# Example: Scrolling through a large file tail -n 50 largefile.log
tail
command examples for different scenarios:
tail
can be used in various scenarios, such as checking log files, monitoring ongoing processes, or viewing recent changes in files.# Example: Using tail for different scenarios tail -f /var/log/auth.log
Troubleshooting common issues with tail
in Linux:
tail
may involve incorrect file paths, permission errors, or issues with the -f
option not displaying updates.# Example: Troubleshooting with tail tail -n 10 /path/to/nonexistent/file