Search code examples
bashtail

How to tail -F, but keep it only scrolling output in last 5 lines


I'd like to be able to tail -F some output, but not have it scrollback the entire buffer, only scroll inside a limited number of lines, say 5 lines.

How can I do this?

I tried

tail -F -n 5 /tmp/dump

but that doesn't seem to work - scrolling lines take up the whole buffer


Solution

  • The following solution isn't pretty - it uses an ANSI escape sequence - but I think it does roughly what you want without using watch:

    while true; do
        tail -5 /tmp/dump | cut -c1-80
        printf '\e[5A'
        sleep 1
    done
    

    The sequence \e[5A means go up five lines. The 5 can be replaced with whatever number you'd like.

    That said, you'd be better off using a curses-like library for this kind of thing. Using raw ANSI escape sequences isn't portable. tput is avaiable in Linux and Cygwin. The cuu capability moves up lines.

    while true; do
        tail -5 /tmp/dump | cut -c1-80
        tput cuu 5
        sleep 1
    done