HOW TO USE TAIL COMMAND

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

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      [email protected]

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      [email protected]

==> cop.txt <==
Name         Email 
Mostafa      [email protected]
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
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Mel
Melhttps://unixcop.com
Unix/Linux Guru and FOSS supporter

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook