How can I continuously print the last line of a file to a single line in the terminal?

The following works, but has a high performance hit.

while true; do tail -1 /tmp/somelog | tr "\012" "\015"; done
  • sleep in the loop, assuming the update frequency isn't too high. – nohillside Jul 15 '18 at 12:04
  • @nohillside: Yup, I thought about that. – forthrin Jul 15 '18 at 17:13

tail already has a -f (--follow) option to poll files for appended content - the trick is to prevent the output from being buffered when you add a pipe to do the line ending replacement:

tail -n1 -f /tmp/somelog | stdbuf -o0 tr '\n' '\r'

For a discussion of the buffering issue see for example Piping tail -f into awk

  • Interesting! However, this doesn't seem to work here. I'm on a BSD variant (macOS), so I had to install coreutils for stdbuf, but I get no output in the terminal when appending data to the log. Is there any way to make this work with, or preferably without stdbuf? – forthrin Jul 15 '18 at 17:17
  • @forthrin sorry I don't know - let's see if any MacOS / BSD specialists can shed light on it – steeldriver Jul 15 '18 at 17:48
  • Seems to work nicely out of the box on Debian. However, there is the pesky issue where lines longer than the terminal cause flooding. How can I make stdbuf take cut -c 1-$(tput cols) | tr '\n' '\r' as an argument? – forthrin Jul 15 '18 at 18:13

Use watch:

watch -t -n0.2 tail -n 1 /var/log/apache2/access.log

-t turns off the title

-n is the refresh interval

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.