
Introduction
The tail command in Linux displays the last parts of a file. The command helps you to monitor log files and view the most recent file changes in real time. The tail command supports various options that modify how the command processes and displays the output. These options are useful when monitoring and troubleshooting system issues.
In this article, you'll use the tail command in Linux to view and monitor file contents.
The tail Command Syntax
The following is a basic tail command syntax:
$ tail [options] [file]
In the above command:
[options]: Accepts optional flags that modify the command's behavior.[file]: Specifies the target file you want to process.
The tail command reads content from the standard input (STDIN) if you don't specify a file, allowing you to redirect the command's output to other commands.
Check out how to use the tar command in Linux for quick file archiving and unzipping.
Most Common Linux tail Command Options
The following are the most common Linux tail command options:
| Option | Description |
|---|---|
-n |
Displays the last n lines of a file. |
-f |
Appends the content of a file as it grows. |
-c |
Displays a file's last n bytes. |
-q |
Quiet mode. Never outputs headers giving file names. |
-v |
Verbose mode. It always outputs headers giving file names. |
--pid |
Terminates after process ID dies when used with -f. |
-s |
Sleeps for a specified number of seconds between iterations when used with -f. |
--retry |
Keeps trying to open a file that doesn't exist or is inaccessible, useful in combination with -f. |
--max-unchanged-stats |
Reopens a file that has not changed size after n iterations when used with -f. |
Practical tail Command in Linux with Examples
This section uses practical examples to show you how to use the Linux tail command. Follow the steps below to run the samples.
Display the last lines in the
/var/log/syslogfile. By default, the tail command displays the last10lines.console$ sudo tail /var/log/syslog
Output:

Display the last
nlines in the/var/log/syslogfile. For example,20lines. The-noption overrides the default value of10lines.console$ sudo tail -n 20 /var/log/syslog
Output:

Display the last
100bytes in the/var/log/syslogfile. For example,100bytes.console$ sudo tail -c 100 /var/log/syslog
Output:

Monitor multiple files using the
-foption and display new lines in real-time.console$ sudo tail -f /var/log/syslog /var/log/auth.log
Output:

Press Ctrl + C to stop the
tailcommand output.Monitor the
/var/log/auth.logfile and specify the sleep interval between updates. For example,2seconds. This option is useful when monitoring active log files to reduce the system load.console$ sudo tail -f -s 2 /var/log/auth.log
Output:

Monitor the
/var/log/syslogfile until a specific process ID (For instance,1234) terminates. This command is useful for automatically monitoring logs attached to a specific process.console$ sudo tail -f --pid=1234 /var/log/syslog
Output:

Display the last lines of a file and stop after
10unchanged iterations. The--max-unchanged-statsoption limits the number of times thetailcommand reopens the/var/log/syslogfile if it detects no changes in the size.console$ sudo tail -f --max-unchanged-stats=10 /var/log/syslog
Output:

Advanced Linux tail Command Usage
The Linux tail command supports more advanced options to customize the output, as illustrated below:
Skip
nnumber of lines from the start of the/var/log/syslogfile using a positive number. For instance, display all lines starting from the10thline in the file.console$ sudo tail -n +10 /var/log/syslog
Output:

Pipe the
tailcommand output to thegrepcommand to search for specific contents likeMACto filter log updates in real-time.console$ sudo tail -f /var/log/syslog | grep 'MAC'
Output:

Monitor multiple files and display headers for clarity. Showing headers makes it clear from which file each line comes from when monitoring multiple files.
console$ sudo tail -f -v /var/log/syslog /var/log/auth.log
Output:

Use the
--retryoption to handle inaccessible files. For instance, retry opening the/var/log/syslogfile if it's inaccessible. The option is useful when working with log files that are not available immediately or frequently rotated.console$ sudo tail -f --retry /var/log/syslog
Output:

Automate Scripts with Linux tail Command
You can use the tail command with scripts to monitor log files and act based on certain conditions. Follow the steps below to create a sample script that checks for a specific string in a log file's last 100 lines.
Create a new sample script file. For instance,
check_log.shusing a text editor likenano.console$ nano check_log.sh
Add the following contents to the
check_log.shfile.bash#!/bin/bash if tail -n 100 /var/log/syslog | grep -q 'error'; then echo "Error found in log file" fi
Save and close the file.
Enable execute permissions on the file.
console$ chmod +x check_log.sh
Run the script.
console$ sudo bash check_log.sh
The above script uses the
tailcommand to get the last 100 lines in a log file and pipes the output to thegrepcommand to search for theerrorstring. If the script finds the string, it prints theError found in log filemessage, which is useful when automating log monitoring tasks.
Use the tail Command Interactively
You can use the tail command with other commands to process the output further. Follow the steps below.
Combine
tailwithawkto extract specific fields from the last few lines of a file. For instance, use the command to display and print the second field (usually the date or time) from the last10lines of the/var/log/sysloglog file.console$ sudo tail -n 10 /var/log/syslog | awk '{print $2}'
Output:

Combine
tailwithsortto filter the last lines of a file. For instance, sort the last15lines of the/var/log/syslogfile.console$ sudo tail -n 15 /var/log/syslog | sort
Output:

Combine
tailwithxargsto delete files listed in the lastnnumber of lines. For instance, display the contents of abackup.shfile and remove the last 5 files listed in the file.console$ sudo tail -n 5 backup.sh | xargs rm
Output:

Combine
tailwithheadto monitor the latest log entries. For instance, display the last50lines of the/var/log/syslogfile and extract the first 10 lines, effectively showing lines41through50.console$ sudo tail -n 50 /var/log/syslog | head -n 10
Output:

Monitor Log Files in Real Time Using tail
Another useful tail command option is the -f option that monitors files in real-time. Follow the steps below.
Monitor the
/var/log/syslogfile and constantly display new entries in real-time. This option is useful when tracking system events and diagnosing issues as they occur.console$ sudo tail -f /var/log/syslog
Output:

Press Ctrl + C to stop monitoring the file changes.
Monitor webserver logs if available. For example, display Nginx webserver log updates in real-time from the
/var/log/nginx/access.logfile.console$ sudo tail -f /var/log/nginx/access.log
Output:

Conclusion
You have used the tail command in Linux to display the last parts of files. The tail command is valuable when monitoring and troubleshooting the Linux system. You can efficiently track file updates, filter output, and automate monitoring tasks in your Linux environment using the tail command. For more information, run man tail to view more command options.