View only the new entries in a growing log file

tail -f x.log

I use this command to see a growing log file in the command prompt.

I am interested only in seeing the log lines that are written to the file after running tail -f and not interested in the logs that were written to the file before doing tail -f. But tail -f command on start, takes the last 10 lines and displays it.

This confuses me, at times if these logs are freshly generated (or) they are old logs?

So, how can i customize tail -f to output only the new entries?

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

You can try:

tail -n0 -f x.log

From man page:

-n, –lines=K
output the last K lines, instead of the last 10; or use -n +K to output lines starting with the Kth

Method 2

You can use the -n0 option to show 0 lines. So

tail -n0 -f some.log

Method 3

You can also use less command like

  1. less file_name.log (log file will be opened and will show first page of the log file)
  2. press shift + f (to act same as like tail -f)
  3. In case if you need to search for any keyword in log file. press ctrl + c to exit the tail mode and search for the keyword as /keyword and ?keyword.

Method 4

I’ve created this approach when I had to monitor entries inside docker container:

LOG_COUNT=0
LOGS=/var/logs/your_logs

function HEART_BEAT() {
    LOG_COUNT_NEW=$(wc -l $LOGS | awk '{ print $1 }')
    tail -n $(( $LOG_COUNT_NEW - $LOG_COUNT )) $LOGS
    LOG_COUNT=$LOG_COUNT_NEW
}

Hope this helps!


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

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments