How to Output Most Recent 10 Lines of Command Output

shelltailterminal

I have a command that outputs thousands of lines. I actually need these lines in order to see the progress of the command. But I do not need to see more than the 10 most recent lines.

How can I do that?

I already tried using a pipe and tail. But it did not work. I just got the last ten lines after the original command finished:

whatever command that has too much output | tail -f

It is important that the console does not get cleared or something like this because there is some important infomation that gets printed right before the command with the lengthy output.
An example:

echo "Very important information. MUST BE VISIBLE!"

# This gives me about 10,000 lines of output pretty fast!
# This output should be shrinked down to the most recent 10
tar -cvf "Bckup.tar" "folder to backup/"

# More code

I hope this clears it up.

EDIT:

The problem with multitail is that it takes up the entire screen. So if I had more than just one output (which I have. Multiple commands with important information are run before this. And I need to use it multiple times).

Something like running this in screen with just 10 lines to display would be perfect. (I know that does not work).

You could imagine that like there is some output from other commands. Then I call the command with the lengthy output. This output stays in a screen that only can display something like 10 lines. After the command is finished more output goes to the console and this should be right beneth it like normal output.

Output
Important Information
Other commands output
----------------------------------------
some lines for the tar command





----------------------------------------
More output
....
....

(Just without the lines)

Best Answer

There is an escape sequence for ECMA-48 (vt100-ish) terminals that restricts scrolling to a subset of lines:

CSI top ; bottom r

Here's a demonstration

# Start with the screen clean and cursor on line 1
clear

# print the non-scrolling banner line
echo Hello world

# set up the scrolling region to lines 2 through 11 and position the
# cursor at line 2
echo -n '^[[2;11r^[[2H'

# Run a command that will produce more than 10 lines, with a slight delay
# so it doesn't happen so fast you can't see it scrolling
perl -MTime::HiRes=sleep -le 'for(1..100) { print; sleep 0.05; }'

# Restore normal scrolling and put cursor on line 12
echo -n '^[[r^[[12H'

Note: All instances of ^[ in the above must be actual escape characters in the script. In vi, for example, they are entered in inset mode with CtrlV followed by Esc.

Related Question