When using tail -f to display the last few lines of a file, how can I make it only ever display 5 lies, overwriting previous lines when the file is added to?

  • 2
    A simple solution is to write into a terminal window with 5 lines. – sudodus Jan 5 '17 at 22:08
  • @sododus What about a convoluted solution? – Ginger Jan 5 '17 at 22:09
  • Please explain what you need, how you intend to use it? – sudodus Jan 5 '17 at 22:12
  • I'm using tail -f on a linode server. The process is active so I get a page of text every second, and it seems to hang a lot. If I could just view the last 5 lines, that would be enough to let me know that the process is running ok. – Ginger Jan 5 '17 at 22:14
  • tail outputs continuously. It doesn't clear the the terminal and cannot overwrite already existing lines. I'd say what you're seeking cannot be done, at least not with tail itself – Sergiy Kolodyazhnyy Jan 5 '17 at 22:55

Suppose you want to watch the file "file-to-be-watched", the following may do the trick:

watch -n 1 tail -5 file-to-be-watched

Are you working at a text mode console, or for example via ssh from a desktop environment?

I made a small demo with date which uses xterm.

xterm -geometry 60x6 -e bash -c 'while true; do dat=$(date "+%H:%M:%S");echo "Hello $dat";echo "world $dat";echo "alias $dat";echo "blank $dat";echo "      $dat"; sleep 1;done'|tail -f

You should be able to replace the while loop with your program, that prints lines every second

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.