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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
It might suffice to use watch:
$ watch tail -n 15 mylogfile.txt
Method 2
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
Method 3
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.
Method 4
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.
Method 5
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0