110

I have a growing log file for which I want to display only the last 15 lines. Here is what I know I can do:

tail -n 15 -F mylogfile.txt

As the log file is filled, tail appends the last lines to the display.

I am looking for a solution that only displays the last 15 lines and get rid of the lines before the last 15 after it has been updated. Would you have an idea?

  • 28
    Resize your terminal window to 15 lines. – Jonathan Jan 19 '12 at 19:48
  • 1
    That's a good one too. – Marc-Olivier Titeux Jan 20 '12 at 9:08
  • 3
    @Jonathan genius! – Marsellus Wallace Sep 19 '15 at 0:00
  • Couldn't upvote jonathan's comment or it loses the flavor of having 15 upvotes – jdero Aug 19 '16 at 18:24
151

It might suffice to use watch:

$ watch tail -n 15 mylogfile.txt
  • 1
    Great! Thanks a lot. I also learn the 'watch' command in the process. – Marc-Olivier Titeux Jan 19 '12 at 12:41
  • 1
    Be aware that this solution doesn’t work if the file grows more than 15 lines between two watch updates. You can use -n to change this interval. – bfontaine Aug 5 '16 at 10:00
25

If you use watch, try the -n option to control the interval between each update.

Thus, the following would call tail every 2 seconds

$ watch -n 2 tail -n 15 mylogfile.txt

while this one polls it every 1 second

$ watch -n 1 tail -n 15 mylogfile.txt
13

You could stream the logfile running less and pressing SHIFT + F that will stream the file using less. $ less mylogfile.txt Then just press SHIFT + F and it will stream. I think it is convenient for monitoring log files that update.

  • 1
    That's a very nice one! – kaiser Mar 10 '16 at 15:21
8

Maybe you find the -d param handy.

man watch

-d Highlight the differences between successive updates. Option will read optional argument that changes highlight to be permanent, allowing to see what has changed at least once since first iteration.

0

In Solaris, AIX or HPUX or UNIX-like (including Linux) you can use scripts to monitoring logs or anything like that:

while true; 
    clear; 
    do date; 
    echo ;
    echo "MONITORING LOG IN "/path/to/file.log": "; 
    echo "Obs.: Last 20 lines of a logfile:
    echo ;
    tail -20 /path/to/file.log;
    echo ;
sleep 5; 
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.