Is it possible to get the output of a command – for example tar – to write each line of output to one line only?
Example usage:
tar -options -f dest source | [insert trickery here]
and the output would show every file being processed without making the screen move: each output overwrites the last one. Can it be done?
Edit: we seem to have a working answer, but lets take it further:
How about doing the same, but over 5 lines? You see a scrolling output that doesn’t affect the rest of the terminal. I think I’ve got an answer, but I’d like to see what you guys think.
Replace the newlines with carriage returns.
Explanation:
cut -b1-$(tput cols): Truncates the output of tar if it is longer than the terminal is wide. Depending on how little you want your terminal to move, it isn’t strictly necessary.sed -u 'i\\o033[2K': Inserts a line blank at the beginning of each line. The-uoption to sed puts it in unbuffered mode.stdbuf -oL sed 'i\\033[2K'would work equally as well.stdbuf -o0 tr '\n' '\r': Usestrto exchange newlines with carriage returns. Stdbuf makes sure that the output is unbuffered; without the\n‘s, on a line buffered terminal, we’d see no output.echo: Outputs a final newline, so that the terminal prompt doesn’t eat up the final line.For the problem your edit proposes:
Feel free to smush all that onto one line. This actually yields an alternative solution for the original problem:
which neatly relies on nothing except VT100 codes.