HOW TO USE TAIL COMMAND

Introduction

Tail : is built in command on unix systems or unix-like operating systems Like :

  • 386BSD.
  • Arch Linux.
  • AIX.
  • Android.
  • BSD NET/2.
  • Debian.
  • DragonFly BSD.
  • GNU Hurd.

Usage : used to display the bottom lines or bytes of the text files or the ending of piped data.

and it is complementary of Head Command

How to use it ?

  • You can use it to display the ending of large text file Like : systems log .
  • It can read last 10 lines by default :
tail /var/log/user.log
  • Or you can specify the number of lines that you need to display it.
tail -n 2  /var/log/user.log
tail --lines=2  /var/log/user.log

Hint : –lines= number of lines === -n number of lines

OUTPUT:

root@unixcop:~# tail --lines=2  /var/log/user.log
Oct  2 14:09:08 unixcop firefox-esr[2370]: gtk_widget_get_window: assertion 'GTK_IS_WIDGET (widget)' failed
Oct  2 14:09:08 unixcop firefox-esr[2370]: gtk_widget_get_window: assertion 'GTK_IS_WIDGET (widget)' failed
root@unixcop:~# 

  • You can either display bytes from text file
  • bytes mean one character .. i will create text file with 10 char
echo "--9LASTCHAR"> ~/bytes.txt
  • Then i will display the last 8 char using tail
tail -c 10 ~/bytes.txt
tail --bytes=9 ~/bytes.txt

OUTPUT:

root@unixcop:~# tail --bytes=10 ~/bytes.txt
9LASTCHAR
root@unixcop:~# 
  • You can use tail to read from many different files
  • I can use this option for comparison.
tail -q unix.txt cop.txt 
tail --quiet  unix.txt cop.txt
  • I can use tail with option verbose to display the content of files with name of the file above
tail -v unix.txt 
tail --verbose unix.txt 

OUTPUT:

==> unix.txt <==
Name         Email 
Mostafa      Moo@unixcop.com

I will merge between verbose option and quiet option to clarify the usage of them

OUTPUT:

root@unixcop:~# tail --quiet --verbose unix.txt cop.txt 
==> unix.txt <==
Name         Email 
Mostafa      Moo@unixcop.com

==> cop.txt <==
Name         Email 
Mostafa      Moo@unixcop.com
root@unixcop:~# 
  • I can use it with pipeline
cat /var/log/user.log | tail -n 3
ls -lah /root | tail -n 5
  • Here I can use tail to store the result of command in text file
cat /var/log/user.log | tail -n 3 > output.txt

OUTPUT:

root@unixcop:~# cat /var/log/user.log | tail -n 3 > output.txt
root@unixcop:~# cat output.txt 
Oct  2 14:08:55 unixcop firefox-esr[2370]: gtk_widget_get_window: assertion 'GTK_IS_WIDGET (widget)' failed
Oct  2 14:09:08 unixcop firefox-esr[2370]: gtk_widget_get_window: assertion 'GTK_IS_WIDGET (widget)' failed
Oct  2 14:09:08 unixcop firefox-esr[2370]: gtk_widget_get_window: assertion 'GTK_IS_WIDGET (widget)' failed
root@unixcop:~# 
  • You can know the tail version by typing :
tail --version 
  • For more help you can use this option:
tail --help 

Conclusion:

This article clarify how to use tail command with practical examples.

For more Information use : man tail
Melhttps://unixcop.com
Unix/Linux Guru and FOSS supporter

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

You might also likeRELATED

How to Install and Configure VNC on Ubuntu Server 20.04

This article will show you how to set up a VNC on Ubuntu Server 20.04. Virtual Network Computing (VNC) is a graphical desktop sharing technology...

How to Export Your Servers Logs with Rsyslog in Centos 8

In this post, you will learn how to Export Your Servers Logs with Rsyslog in Centos 8 In this article, we'll walk through setting up...

How To Setup a Counter Strike: Global Offensive Server on CentOS 8

Counter-Strikes first option is a feature called "Official Matchmaking." This selects a Steam-owned server near you where you can play against players from all...