How to best capture and log scp output?

scp prints its progress bar to the terminal using control codes. It will detect if you redirect output and thus omit the progress bar.

You can get around that by tricking scp into thinking it runs in a terminal using the "script" command which is installed on most distros by default:

script -q -c "scp server:/file /tmp/" > /tmp/test.txt

The content of test.txt will be:

file    0%    0     0.0KB/s   --:-- ETA
file   18%   11MB  11.2MB/s   00:04 ETA
file   36%   22MB  11.2MB/s   00:03 ETA
file   54%   34MB  11.2MB/s   00:02 ETA
file   73%   45MB  11.2MB/s   00:01 ETA
file   91%   56MB  11.2MB/s   00:00 ETA
file  100%   61MB  10.2MB/s   00:06

...which is probably what you want.

I stumbled over this problem while redirecting the output of an interactive script into a log file. Not having the results in the log wasn't a problem as you can always evaluate exit codes. But I really wanted the interactive user to see the progress bar. This answer solves both problems.


It looks like your just missing whether the scp was succesful or not from the log.

I'm guessing the scroll bar doesn't print to stdout and uses ncurses or some other kind of TUI?

You could just look at the return value of scp to see whether it was successful. Like

scp myfile [email protected]:. && echo success!

man scp says

scp exits with 0 on success or >0 if an error occurred.

None of the answers here worked for me, I needed to recursively copy large directory with lot of files over long geo distance, so I wanted to log the progress (&& echo success! was by far not enough).

What I finally engineered and somehow worked was:

scp -vrC root@host:/path/to/directory . 2> copy.log &

With -v doing the trick of verbose logging (-C allows compression and -r recursion).

Grepping the logfile

grep file copy.log | wc -l

allowed me to see the number of files copied so far.

Tags:

Logging

Shell

Scp