What does the `-C` flag exactly do in `scp`?

I always use either rsync or scp in order to copy files from/to a remote machine. Recently, I discovered in the manual of scp (man scp) the flag -C

 -C      Compression enable.  Passes the -C flag to
         ssh(1) to enable compression.

Before I discovered this flag, I used to zip before and then scp.

Is it as efficient to just use the -C than zipping and unzipping? When is using one or another process make the transfer faster?

Asked By: Remi.b

||

It’s never really going to make any big difference, but zipping the file before copying it ought to be a little bit less efficient since using a container format such as zip that can encapsulate multiple files (like tar) is unnecessary and it is not possible to stream zip input and output (so you need a temporary file).

Using gzip on the other hand, instead of zip ought to be exactly the same since it’s what ssh -C does under the hood… except that gzipping yourself is more work than just using ssh -C.

Answered By: Celada

It enables the gzip compression in ssh (under the scp).

On slow connections this will speed things up, on any reasonably fast connection ( 100Mbit or faster) the compression is very likely to slow things down.

It will be more or less efficient than zip based on whether gzip (specifically gzip -6) would be more or less efficient than your chosen zip compression level

Answered By: Wayne Walker

The -C flag enables a gzip compression of an SSH stream.

It’s an equivalent of Accept-Encoding: gzip in HTTP.

How the flag performs depends on a kind of data you transfer:

  • When transferring a single large file, the performance would be near the same to zipping the file before the transfer (neglecting efficiency of zip vs. gzip algorithm).

    But using -C is a less effort for you as an user.

  • When transferring a lots of small files, the performance will be inferior to zipping the files before transfer.

    A reason behind that is, that before each file transfer, there’s an interactive communication between the SCP server and the client (for exchanging file metadata, like timestamp and permissions). So both sides have to wait a bit for the other side to respond (compression won’t help while waiting). That’s a wasted time for each transferred file. How much time is wasted depends on a latency of the connection. In the end, the transfer can be magnitudes slower.

    When you transfer a single zipped file, that communication happens only once.

Answered By: Martin Prikryl
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.