Bash – How to limit the number of lines a command’s output has available in bash

bashnohupstdouttail

I started downloading a big file in the background using

$ nohup wget http://example.tld/big.iso &

which also gives me a nohup.out file that includes the output of wget.

Now, if I later want to watch the downloading process, I could use $ tail -f nohup.out but that fills up my terminal window faster than I'd wish for. What I'd like to see is the last line constantly updating (just like when using wget alone).

I tried $ tail -n 1 -f nohup.out but it seems to affect only the initial tailin'.

Generally speaking, if it is possible to limit (in this case to 1) the number of lines a command's output has available/visible it would solve this problem. Sort of having the output in a Circular buffer — just think of the normal progress bar $ wget example.tld/big.iso would print.

Is there such a solution?

Or am I climbing the tree wrong way? (Meaning, would it be easier to limit nohup's output or do something else?)

Best Answer

If you do not want to limit the scrolling region (see my other answer), you can also use the carriage return to go back to the beginning of the line before printing the next line. There is an escape sequence that clears the rest of the line, which is necessary when the current line is shorter than the previous line.

nohup wget http://cdimage.debian.org/debian-cd/6.0.3/amd64/iso-dvd/debian-6.0.3-amd64-DVD-1.iso &
el="$(tput el)"; # Clear to the end of the line
tail -n 1 -f nohup.out | while read -r line; do echo -n $'\r'"$el$line"; done;