As Linux users, we often work with long-running background Linux processes, which are called daemons or services. Some of the common examples of the services are Secure Shell (sshd), Network Manager (networkd), Volume Manager (LVM), Cron, and the list goes on.
Many times we need to monitor the logs of these services to debug the system issues. However, one of the main challenges is that these services generate a lot of logs and most of the time going through these logs makes it cumbersome, this is where we can use the tail command.
tail command is a command-line utility, similar to the head command that reads a file and prints the last 10 lines (content) of one or more files to standard output.
In this practical guide, we will learn about the tail command. By the end of this guide, Linux command-line users will be able to use the tail command effectively.
Table of Contents
tail Command Syntax
The syntax of the tail command is similar to other Linux commands:
$ tail [OPTIONS] [FILE-1] [FILE-2] ...
1. Print Last 10 Lines Of File in Linux
By default, the tail command prints the last 10 lines of the given file as shown.
<strong>$ tail /var/log/secure</strong> Apr 2 14:17:24 TecMint sshd[201178]: Disconnected from user tecmint 192.168.0.162 port 59774 Apr 2 14:17:24 TecMint sshd[201165]: pam_unix(sshd:session): session closed for user tecmint Apr 2 14:29:12 TecMint sshd[201366]: Accepted password for tecmint from 192.168.0.162 port 56378 ssh2 Apr 2 14:29:12 TecMint systemd[201371]: pam_unix(systemd-user:session): session opened for user tecmint(uid=1002) by (uid=0) Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session opened for user tecmint(uid=1002) by (uid=0) Apr 2 14:29:12 TecMint sshd[201382]: Received disconnect from 192.168.0.162 port 56378:11: disconnected by user Apr 2 14:29:12 TecMint sshd[201382]: Disconnected from user tecmint 192.168.0.162 port 56378 Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session closed for user tecmint Apr 2 15:12:55 TecMint sshd[202049]: Accepted password for root from 192.168.0.162 port 53334 ssh2 Apr 2 15:12:55 TecMint sshd[202049]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
Here, we can see that the above command shows the last ten lines from the /var/log/secure file.
2. Print Last N Lines of File in Linux
In the last example, the command prints the last 10 lines of the given file. However, we can use the -n
option which allows us to limit the number of lines to be printed on the screen as shown.
<strong>$ tail -n 3 /var/log/secure</strong> Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session closed for user tecmint Apr 2 15:12:55 TecMint sshd[202049]: Accepted password for root from 192.168.0.162 port 53334 ssh2 Apr 2 15:12:55 TecMint sshd[202049]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
In this example, we can see that now the command shows the last three lines only instead of the ten lines.
3. Ignore First N Lines of a File in Linux
Here, we can use the plus ( )
symbol with the -n
option, which allows us to control the starting point from the given file.
To understand this, let’s use the 5
value to start the output from the 5th line:
<strong>$ tail -n 5 /var/log/secure</strong> Apr 2 14:17:24 TecMint sshd[201178]: Disconnected from user tecmint 192.168.0.162 port 59774 Apr 2 14:17:24 TecMint sshd[201165]: pam_unix(sshd:session): session closed for user tecmint Apr 2 14:29:12 TecMint sshd[201366]: Accepted password for tecmint from 192.168.0.162 port 56378 ssh2 Apr 2 14:29:12 TecMint systemd[201371]: pam_unix(systemd-user:session): session opened for user tecmint(uid=1002) by (uid=0) Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session opened for user tecmint(uid=1002) by (uid=0) Apr 2 14:29:12 TecMint sshd[201382]: Received disconnect from 192.168.0.162 port 56378:11: disconnected by user Apr 2 14:29:12 TecMint sshd[201382]: Disconnected from user tecmint 192.168.0.162 port 56378 Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session closed for user tecmint Apr 2 15:12:55 TecMint sshd[202049]: Accepted password for root from 192.168.0.162 port 53334 ssh2 Apr 2 15:12:55 TecMint sshd[202049]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
4. Show Last N Characters of the File
Similar to lines, we can also use the command to display the last N
characters of the file using the -c
option as shown below:
<strong>$ tail -c 7 /var/log/secure</strong> (uid=0)
In this example, we can see that the command shows the last seven ASCII characters of the given file.
5. Remove First N Characters of File
Similarly, we can use the plus symbol ( )
with the -c
option to skip the first N
character. So let’s skip the first line of the file using the below command:
<strong>$ tail -c 5 /var/log/secure</strong> Apr 2 03:02:59 TecMint sudo[162801]: root : TTY=pts/2 ; PWD=/root ; USER=root ; COMMAND=/bin/dnf install R Apr 2 03:02:59 TecMint sudo[162801]: pam_unix(sudo:session): session opened for user root(uid=0) by root(uid=0) Apr 2 03:03:02 TecMint sudo[162801]: pam_unix(sudo:session): session closed for user root Apr 2 03:11:17 TecMint groupadd[163602]: group added to /etc/group: name=avahi, GID=70 Apr 2 03:11:18 TecMint groupadd[163602]: group added to /etc/gshadow: name=avahi Apr 2 03:11:18 TecMint groupadd[163602]: new group: name=avahi, GID=70 Apr 2 03:11:19 TecMint useradd[163610]: new user: name=avahi, UID=70, GID=70, home=/var/run/avahi-daemon, shell=/sbin/nologin, from=none Apr 2 03:13:41 TecMint groupadd[163704]: group added to /etc/group: name=colord, GID=986 Apr 2 03:13:41 TecMint groupadd[163704]: group added to /etc/gshadow: name=colord
Here, we can see that the command shows all the lines except the first line.
6. Show File Name in Header
We can instruct the tail command to display the current file name as a display header, which comes in handy while working with multiple files.
So, let’s use the -v
option to enable the display header:
<strong>$ tail -n 3 -v /var/log/secure</strong> ==>/var/log/secure <p>In the above output, <code>==> /var/log/secure represents the display header.</code></p> <h3> <span class="ez-toc-section" id="7_Show_File_Name_as_Header_in_Multiple_Files"></span>7. Show File Name as Header in Multiple Files<span class="ez-toc-section-end"></span> </h3> <p>Just like any other file-processing command, we can also use multiple files with the <strong>tail</strong> command. In such cases, the display header gets used to separate the file contents.</p> <pre class="brush:php;toolbar:false"><strong>$ tail -n 3 -v /var/log/secure /var/log/secure-20230402</strong> ==> /var/log/secure /var/log/secure-20230402 <p>In the above output, we can see the display header for each file.</p> <h3> <span class="ez-toc-section" id="8_How_to_Disable_Display_Header_in_File"></span>8. How to Disable Display Header in File<span class="ez-toc-section-end"></span> </h3> <p>In the previous example, we saw that the command enables the display header while working with multiple files. However, we can suppress this default behavior using the <code>-q</code> option.</p> <pre class="brush:php;toolbar:false"><strong>$ tail -q -n 3 /var/log/secure /var/log/secure-20230402</strong> Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session closed for user tecmint Apr 2 15:12:55 TecMint sshd[202049]: Accepted password for root from 192.168.0.162 port 53334 ssh2 Apr 2 15:12:55 TecMint sshd[202049]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0) Mar 31 03:50:53 TecMint groupadd[156163]: new group: name=docker, GID=987 Mar 31 04:46:11 TecMint sshd[159403]: Accepted password for root from 192.168.0.162 port 46480 ssh2 Mar 31 04:46:11 TecMint sshd[159403]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
Here, we can see that now the command displays the file contents one after another without any display header.
9. How to Watch a File for Changes
So far we saw that the tail command exits once it processes the required number of lines or characters. However, sometimes we want to view the newly generated logs as well.
In such cases, we can use the -f
option with the command, which allows us to monitor the file for changes in a real-time.
To understand this, first, let’s execute the below command in the first terminal:
<strong>$ tail -f /var/log/messages</strong> Apr 2 15:13:28 TecMint NetworkManager[741]: [1680462808.8441] policy: set-hostname: current hostname was changed outside NetworkManager: 'TecMint' Apr 2 15:13:28 TecMint systemd[1]: Starting Network Manager Script Dispatcher Service... Apr 2 15:13:28 TecMint systemd[1]: Started Network Manager Script Dispatcher Service. Apr 2 15:13:37 TecMint arpwatch[11001]: rename arp.dat -> arp.dat-: Operation not permitted Apr 2 15:13:38 TecMint systemd[1]: NetworkManager-dispatcher.service: Deactivated successfully. Apr 2 15:13:58 TecMint systemd[1]: systemd-hostnamed.service: Deactivated successfully. Apr 2 15:18:03 TecMint systemd[1]: Starting dnf makecache... Apr 2 15:18:03 TecMint dnf[202235]: Metadata cache refreshed recently. Apr 2 15:18:03 TecMint systemd[1]: dnf-makecache.service: Deactivated successfully. Apr 2 15:18:03 TecMint systemd[1]: Finished dnf makecache.
Here, we can see that the command is waiting infinitely after displaying the last ten lines:
Next, let’s open another terminal and append some text to the numbers-2.txt file:
$ echo "View Logs in Real-Time" >> /var/log/messages
Now, let’s switch to the first terminal to view the newly added text:
<strong>$ tail -f /var/log/messages</strong> Apr 2 15:13:28 TecMint NetworkManager[741]: [1680462808.8441] policy: set-hostname: current hostname was changed outside NetworkManager: 'TecMint' Apr 2 15:13:28 TecMint systemd[1]: Starting Network Manager Script Dispatcher Service... Apr 2 15:13:28 TecMint systemd[1]: Started Network Manager Script Dispatcher Service. Apr 2 15:13:37 TecMint arpwatch[11001]: rename arp.dat -> arp.dat-: Operation not permitted Apr 2 15:13:38 TecMint systemd[1]: NetworkManager-dispatcher.service: Deactivated successfully. Apr 2 15:13:58 TecMint systemd[1]: systemd-hostnamed.service: Deactivated successfully. Apr 2 15:18:03 TecMint systemd[1]: Starting dnf makecache... Apr 2 15:18:03 TecMint dnf[202235]: Metadata cache refreshed recently. Apr 2 15:18:03 TecMint systemd[1]: dnf-makecache.service: Deactivated successfully. Apr 2 15:18:03 TecMint systemd[1]: Finished dnf makecache. <strong>View Logs in Real-Time</strong>
Here, we can see that the tail command shows the newly added text.
Do you know of any other best example of the tail command in Linux? Let us know your views in the comments below.
The above is the detailed content of How to Use Tail Command in Linux with Examples. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Are you looking for good software to write mathematical equations? If so, this article provides the top 5 equation editors that you can easily install on your favorite Linux distribution.In addition to being compatible with different types of mathema

Linux administrators should be familiar with the command-line environment. Since GUI (Graphical User Interface) mode in Linux servers is not commonly installed.SSH may be the most popular protocol to enable Linux administrators to manage the servers

Gogo is a remarkable tool to bookmark directories inside your Linux shell. It helps you create shortcuts for long and complex paths in Linux. This way, you no longer need to type or memorize lengthy paths on Linux.For example, if there's a directory

PPA is an important tool for Ubuntu users to expand their software sources. 1. When searching for PPA, you should visit Launchpad.net, confirm the official PPA in the project official website or document, and read the description and user comments to ensure its security and maintenance status; 2. Add PPA to use the terminal command sudoadd-apt-repositoryppa:/, and then run sudoaptupdate to update the package list; 3. Manage PPAs to view the added list through the grep command, use the --remove parameter to remove or manually delete the .list file to avoid problems caused by incompatibility or stopping updates; 4. Use PPA to weigh the necessity and prioritize the situations that the official does not provide or require a new version of the software.

LXD is described as the next-generation container and virtual machine manager that offers an immersive for Linux systems running inside containers or as virtual machines. It provides images for an inordinate number of Linux distributions with support

How to quickly generate test files of a specified size? It can be achieved using command line tools or graphical software. On Windows, you can use fsutilfilecreatenew file name size to generate a file with a specified byte; macOS/Linux can use ddif=/dev/zeroof=filebs=1Mcount=100 to generate real data files, or use truncate-s100M files to quickly create sparse files. If you are not familiar with the command line, you can choose FSUtilGUI, DummyFileGenerator and other tool software. Notes include: pay attention to file system limitations (such as FAT32 file size upper limit), avoid overwriting existing files, and some programs may

The key to installing dual systems in Linux and Windows is partitioning and boot settings. 1. Preparation includes backing up data and compressing existing partitions to make space; 2. Use Ventoy or Rufus to make Linux boot USB disk, recommend Ubuntu; 3. Select "Coexist with other systems" or manually partition during installation (/at least 20GB, /home remaining space, swap optional); 4. Check the installation of third-party drivers to avoid hardware problems; 5. If you do not enter the Grub boot menu after installation, you can use boot-repair to repair the boot or adjust the BIOS startup sequence. As long as the steps are clear and the operation is done properly, the whole process is not complicated.

Node Version Manager (NVM) is a simple bash script that helps manage multiple Node.js versions on your Linux system. It enables you to install various Node.js versions, view available versions for installation, and check already installed versions.NV
