Cyberithub

12 Best Examples of tail command in Linux for Beginners

Advertisements

In this article, I will take you through 12 Best Examples of tail command in Linux for Beginners. tail command is an open source tool in Linux used generally used to view the end section of the files. Most of the times used along with head command for efficiently viewing a particular section of the file. By default it will show the last 10 lines of the file. You can find many uses of tail command in Linux particularly in troubleshooting purposes.

In this section, we will go through multiples examples of tail command in Linux to understand its uses and in next article we will cover other important tools like head command in Linux.

SYNTAX

tail [OPTION]... [FILE]...

12 Best Examples of tail command in Linux for Beginners 1

tail command in Linux

Also Read: 10 Best Examples of head command in Linux (RedHat/CentOS 7/8)

Example 1. Check tail command version

If you want to check tail command version then you need to use tail --version command as shown below. As you can check from below output current tail command version is 8.22

[root@localhost ~]# tail --version
tail (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Rubin, David MacKenzie, Ian Lance Taylor,
and Jim Meyering.

--version : output version information and exit

Example 2. Display last 10 lines of sshd_config file

If you want to check the last 10 lines of sshd_config file then you need to use tail /etc/ssh/sshd_config command as shown below. By default tail command in Linux will show the last 10 lines of a file.

[root@localhost ~]# tail /etc/ssh/sshd_config

# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server

Example 3. Display last x Lines of sshd_config file

If you want to display the last 5 lines without using -n option then you need to run tail -5 /etc/ssh/sshd_config command as shown below. You can also use -n option to see the same output by passing number of lines in the argument as shown in the next example.

[root@localhost ~]# tail -5 /etc/ssh/sshd_config
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server

Example 4. Display last x Lines of sshd_config file using -n option

If you want to check the last 3 lines of sshd_config file using -n option, then you need to run tail -n 3 /etc/ssh/sshd_config command as shown below.

[root@localhost ~]# tail -n 3 /etc/ssh/sshd_config
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server

-n : output the last K lines, instead of the last 10

NOTE:

Please note that here I am using root user to run all the below commands.You can use any user with sudo access to run all these commands.For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to User.

Example 5. Display last n bytes of output from sshd_config file

If you want to check the last 30 bytes of sshd_config file then you need to run tail -c 30 /etc/ssh/sshd_config command as shown below.

[root@localhost ~]# tail -c 30 /etc/ssh/sshd_config
no
# ForceCommand cvs server

-c : output the last K bytes

Example 6. Do not remove the headers using tail command in Linux

If you want to see the file contents with headers information then you need to use below tail command in Linux. As you can see from below output we are displaying the contents of file1.txt and file2.txt using tail -v file1.txt file2.txt command.

[root@localhost ~]# tail -v file1.txt file2.txt
==> file1.txt <==
This is cyberithub
Hello World

==> file2.txt <==
This is from cyberithub
Hello from cyberithub

-v : always output headers giving file names

Example 7. Remove the headers using tail command in Linux

If you do not want to display the header of the files along with their contents then you need to use -q option with tail command as demonstrated in below example. Here we have two files file1.txt and file2.txt whose contents are shown below.

[root@localhost ~]# cat file1.txt
This is cyberithub
Hello World
[root@localhost ~]# cat file2.txt
This is from cyberithub
Hello from cyberithub

Now run the tail command in Linux with -q option to display the contents of both the files without showing their header information.

[root@localhost ~]# tail -q file1.txt file2.txt
This is cyberithub
Hello World
This is from cyberithub
Hello from cyberithub

-q : never output headers giving file names

Example 8. Continue display of Log Messages using tail command in Linux

If you want to continuously display the /var/log/messages contents then you need to use tail -f /var/log/messages command as shown below.

[root@localhost ~]# tail -f /var/log/messages
May 1 10:57:20 localhost dhclient[1946]: DHCPDISCOVER on enp0s3 to 255.255.255.255 port 67 interval 12 (xid=0x4a1b83c8)
May 1 10:57:20 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x4a1b83c8)
May 1 10:57:20 localhost dhclient[1946]: DHCPOFFER from 192.168.0.1
May 1 10:57:27 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x4a1b83c8)
May 1 10:57:27 localhost dhclient[1946]: DHCPACK from 192.168.0.1 (xid=0x4a1b83c8)
May 1 10:57:30 localhost NET[29137]: /usr/sbin/dhclient-script : updated /etc/resolv.conf
May 1 10:57:30 localhost dhclient[1946]: bound to 192.168.0.104 -- renewal in 3291 seconds.
May 1 10:59:17 localhost systemd: Started Session 101 of user root.
May 1 10:59:17 localhost systemd-logind: New session 101 of user root.

-f : output appended data as the file grows

Example 9. Check logs after Every n secs

If you want to check the /var/log/messages logs after every 4 seconds then you need to use tail -f -s 4 /var/log/messages command as shown below.

[root@localhost ~]# tail -f -s 4 /var/log/messages
May 1 16:42:43 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x577c2ef)
May 1 16:42:56 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x577c2ef)
May 1 16:43:11 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x577c2ef)
May 1 16:43:32 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x577c2ef)
May 1 16:43:42 localhost systemd: Started Session 108 of user root.
May 1 16:43:42 localhost systemd-logind: New session 108 of user root.
May 1 16:43:46 localhost systemd-logind: Removed session 101.
May 1 16:43:48 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x577c2ef)
May 1 16:43:56 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x577c2ef)
May 1 16:44:05 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x577c2ef)
May 1 16:44:22 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x577c2ef)
May 1 16:44:31 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x577c2ef)

-s : sleep for approximately N seconds (default 1.0) between iterations

Example 10: Display top n lines from sshd_config file using tail command in Linux

If you want to display the top 5 lines of sshd_config file then you need to use below tail command in Linux.

[root@localhost ~]# cat /etc/ssh/sshd_config | head -n 5 | tail
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.

Example 11: Follow Log File /var/log/messages using tail command in Linux

If you want to follow the /var/log/messages logs then you need to use below tail command in Linux.

[root@localhost ~]# tail -F /var/log/messages
May 1 16:57:05 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x577c2ef)
May 1 16:57:22 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x577c2ef)
May 1 16:57:30 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x577c2ef)
May 1 16:57:32 localhost dhclient[1946]: DHCPDISCOVER on enp0s3 to 255.255.255.255 port 67 interval 6 (xid=0x6324e5ee)
May 1 16:57:32 localhost dhclient[1946]: DHCPREQUEST on enp0s3 to 255.255.255.255 port 67 (xid=0x6324e5ee)
May 1 16:57:32 localhost dhclient[1946]: DHCPOFFER from 192.168.0.1
May 1 16:57:33 localhost dhclient[1946]: DHCPACK from 192.168.0.1 (xid=0x6324e5ee)
May 1 16:57:35 localhost NET[14517]: /usr/sbin/dhclient-script : updated /etc/resolv.conf
May 1 16:57:35 localhost dhclient[1946]: bound to 192.168.0.104 -- renewal in 3307 seconds.
May 1 17:01:01 localhost systemd: Started Session 109 of user root.

-F : follow the output

Example 12 : Check other tail command in Linux Options

If you want to check all the other options available with tail command in Linux then you need to run tail --help command as shown below.

[root@localhost ~]# tail --help
Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
-c, --bytes=K output the last K bytes; or use -c +K to output
bytes starting with the Kth of each file
-f, --follow[={name|descriptor}]
output appended data as the file grows;
an absent option argument means 'descriptor'
-F same as --follow=name --retry
-n, --lines=K output the last K lines, instead of the last 10;
or use -n +K to output starting with the Kth
--max-unchanged-stats=N
with --follow=name, reopen a FILE which has not
changed size after N (default 5) iterations
to see if it has been unlinked or renamed
(this is the usual case of rotated log files);
with inotify, this option is rarely useful
--pid=PID with -f, terminate after process ID, PID dies
-q, --quiet, --silent never output headers giving file names
--retry keep trying to open a file if it is inaccessible
-s, --sleep-interval=N with -f, sleep for approximately N seconds
(default 1.0) between iterations;
with inotify and --pid=P, check process P at
least once every N seconds
-v, --verbose always output headers giving file names
--help display this help and exit
--version output version information and exit

 

 

Popular Recommendations:-

tail command in Linux Examples

How to Install locate command on Linux (RedHat/CentOS 7/8)

How to Install netcat(nc) command on Linux (RedHat/CentOS 7/8) in 5 Easy Steps

9 useful w command in Linux with Examples

12 Most Popular rm command in Linux with Examples

Create a Self Signed Certificate using OpenSSL

Leave a Comment