pmhaynes

Just another WordPress site

  • Buy Adspace

How to use “Tail” Command

Posted by Paul on April 29, 2014
Posted in: Linux. Tagged: tail.

Have you ever needed to look at the last entries in a log file.

Of course you have.

For example you want to see the last errors in the /var/logs/mail.log file

If you open it with NANO or some other txt editing programme you will have to scroll all the way to the bottom.

AND if more entries are logged while you are already in the file you will not see them!

This is a pain

“Tail” is very useful because in its simplest form

tail /var/logs/mail.log

It will show you the last 10 lines of txt form the log file

 

If you add the -f command it will follow the file. Meaning if any lines are added to the log file they will appear real time

tail -f /var/logs/mail.log

Use the “-F” option to force tail to follow file names rather than file objects. This can prevent problems with log rotation and other programs that may alter file names.

 

Filter Lines in Followed Logs with grep

The grep tool can be combined with tail to filter the contents of a log file in real time. Consider the following examples:

tail -F procmail.log | grep -e “^Subject”
tail -F access.log | grep “404”
In the first command, only the lines of the procmail.log file that begin with the characters “Subject” are printed. All other lines are discarded.

In the second invocation, only entries from the access log that contain the characters “404” will be printed. All other lines are discarded.

 
-n, –lines=N
output the last N lines, instead of the last 10

–retry, keep trying to open a file even if it is inaccessible when tail starts or if it becomes inaccessible later — useful only with -f

 

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Like this:

Like Loading...

Posts navigation

← Windows Service stuck stopping or starting
How to use “grep” →
  • Advert

  • Recent Posts

    • MS Office 2010 working links on Microsoft URL
    • Change your LAN from public to Private Profile with Powershell on Windows 10
    • How to change WordPress upload size limit
    • Routing network traffic between 2 subnets using a Raspberry Pi
    • OpenVPN setup
  • Recent Comments

    • Archives

      • March 2023 (2)
      • June 2021 (1)
      • February 2019 (1)
      • December 2018 (1)
      • March 2018 (2)
      • April 2015 (2)
      • January 2015 (1)
      • October 2014 (1)
      • June 2014 (1)
      • May 2014 (6)
      • April 2014 (2)
      • March 2014 (1)
      • January 2014 (1)
      • December 2013 (1)
      • November 2013 (6)
    • Categories

      • Linux
      • Uncategorized
      • Windows
      • Wordpress
    • Meta

      • Log in
      • Entries feed
      • Comments feed
      • WordPress.org
    Proudly powered by WordPress Theme: Parament by Automattic.
    %d