Useful tail command with examples in Linux/Unix

In this tutorial we will learn about useful tail command with examples in Linux/Unix which will help you in linux and Unix system administration.

Question: What the tail command do
Answer: tail command helps to get output from last part of file.

Question: When we generally use the tail command.
Answer:
=> tail command is very helpful when you want to see some last part of file means upto some last line numbers of file.
=> It is very useful when the file size is big and we require only to see the output of file up to some last line numbers of file. (For eg. if file size is big lets assume more than 1GB in that case using cat or any editor(vi/nano) command is bad idea.If you use these command in such cases, it will take lot of time to open).
=> If you want to see last lines of log files which is continuously writing and running.For eg. apache access log of busy web server.

Note: By defualt tail command show output of last 10 lines of file

Command 1: You want to see output of last 10 lines of file (if you use only tail command without any switches it show by-default 10 last lines)

Syntax: tail file_name

Example: Here, we are using tail command and file name is test.

root@tuxworld:~# tail test
  libboost-program-options1.46.1 libboost-signals1.46.1 libboost-thread1.46.1 libcdt4 libcelt0-0 libclutter-imcontext-0.1-0
  libclutter-imcontext-0.1-bin libcluttergesture-0.0.2-0 libcmis-0.2-0 libdevhelp-3-0 libexiv2-11 libgdata1.9-cil libgegl-0.0-0 libglew1.6
  libglewmx1.6 libgraph4 libgvc5 libktorrent3 libkworkspace4abi1 libllvm3.0 libmagick++4 libmagickcore4 libmagickcore4-extra libmagickwand4
  libnepomukdatamanagement4 libntfs10 libnux-2.0-0 libnux-2.0-common libopal3.10.2 libopenspc0 libpathplan4 libpoppler19 libpt2.10.2
  libseed-gtk3-0 libvala-0.16-0 packagekit-backend-aptcc python-gmenu python-launchpad-integration python-packagekit valac-0.16 valac-0.16-vapi
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 218 not upgraded.
root@tuxworld:/etc/pam.d# exit

Script done on Wednesday 13 March 2013 02:48:02 AM IST
root@tuxworld:~# 

Command 2: In this example, we will show output of last lines but the line numbers will vary as you give the desired numbers of lines in switch

Syntax tail -n number_of_lines file_name

Example: Here I want to see output upto 4 last lines.You may increase/decrease the number of lines as per your requirement

root@tuxworld:~# tail -n 4 test
0 upgraded, 0 newly installed, 0 to remove and 218 not upgraded.
root@tuxworld:/etc/pam.d# exit

Script done on Wednesday 13 March 2013 02:48:02 AM IST
root@tuxworld:~# 

Command 3: In this example, we can see last lines of file if the file is continuously writing. Such files you can see in log file because they are continuously writing. For eg. Busy Mail server has mail.log same as Apache Server has httpd.log /Access.log(Naming depends upon Operating System)

Syntax: tail -f file_name
Note: To stop seeing the output of file press combination of “CTRL C” keys from your keyboard
(In MacOS press “Command C” from keyboard)

Example: Here, we are using the mail.log , in below reference see ^C it come after we press the “CTRL C” to stop seeing the output of file

root@tuxworld:~# tail -f /var/log/mail.log
Dec 18 23:42:02 tuxworld postfix/smtp[19527]: connect to example.com[93.184.216.119]:25: Connection timed out
Dec 18 23:42:02 tuxworld postfix/smtp[19529]: connect to example.com[93.184.216.119]:25: Connection timed out
Dec 18 23:42:02 tuxworld postfix/smtp[19529]: connect to example.com[2606:2800:220:6d:26bf:1447:1097:aa7]:25: Network is unreachable
Dec 18 23:42:02 tuxworld postfix/smtp[19520]: connect to example.com[93.184.216.119]:25: Connection timed out
Dec 18 23:42:02 tuxworld postfix/smtp[19520]: connect to example.com[2606:2800:220:6d:26bf:1447:1097:aa7]:25: Network is unreachable
Dec 18 23:42:02 tuxworld postfix/smtp[19527]: EAA1B162A89: to=, relay=none, delay=83254, delays=83223/0.01/31/0, dsn=4.4.1, status=deferred (connect to example.com[93.184.216.119]:25: Connection timed out)
Dec 18 23:42:02 tuxworld postfix/smtp[19520]: 85BC4162A23: to=, relay=none, delay=84301, delays=84270/0.07/31/0, dsn=4.4.1, status=deferred (connect to example.com[2606:2800:220:6d:26bf:1447:1097:aa7]:25: Network is unreachable)
Dec 18 23:42:02 tuxworld postfix/smtp[19529]: DC05D162A8A: to=, relay=none, delay=83254, delays=83223/0.03/31/0, dsn=4.4.1, status=deferred (connect to example.com[2606:2800:220:6d:26bf:1447:1097:aa7]:25: Network is unreachable)
Dec 18 23:42:02 tuxworld postfix/smtp[19528]: connect to example.com[93.184.216.119]:25: Connection timed out
Dec 18 23:42:02 tuxworld postfix/smtp[19528]: 9F58B162528: to=, relay=none, delay=84301, delays=84270/0.02/31/0, dsn=4.4.1, status=deferred (connect to example.com[93.184.216.119]:25: Connection timed out)
^C

For further reading, man tail or tail --help command in terminal

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.