Copy a folder tree through ssh using compression (no temporary files)

ssh <host> 'tar -cz /<folder>/<subfolder>' | tar -xvz
This command will copy a folder tree (keeping the parent folders) through ssh. It will: - compress the data - stream the compressed data through ssh - decompress the data on the local folder This command will take no additional space on the host machine (no need to create compressed tar files, transfer it and then delete it on the host). There is some situations (like mirroring a remote machine) where you simply cant wait for a huge time taking scp command or cant compress the data to a tarball on the host because of file system space limitation, so this command can do the job quite well. This command performs very well mainly when a lot of data is involved in the process. If you copying a low amount of data, use scp instead (easier to type)
Sample Output
#ssh hula2 'tar -cz /export/logs/account-collector/server.log.2009-11-*' | tar -xzv
export/logs/account-collector/server.log.2009-11-01
export/logs/account-collector/server.log.2009-11-02
export/logs/account-collector/server.log.2009-11-03
export/logs/account-collector/server.log.2009-11-04
export/logs/account-collector/server.log.2009-11-05
export/logs/account-collector/server.log.2009-11-06
export/logs/account-collector/server.log.2009-11-07
export/logs/account-collector/server.log.2009-11-08
export/logs/account-collector/server.log.2009-11-09

#ls -R ~/export
/home/user/export:
logs

/home/user/export/logs:
account-collector

/home/user/export/logs/account-collector:
server.log.2009-11-01  server.log.2009-11-03  server.log.2009-11-05  server.log.2009-11-07  server.log.2009-11-09
server.log.2009-11-02  server.log.2009-11-04  server.log.2009-11-06  server.log.2009-11-08

12
By: polaco
2009-11-10 20:06:47

What Others Think

In case you want to use fewer CPU cycles, you could just drop the gzip compression of tar (which is sometimes the bottleneck depending on the data), or use a different cipher in ssh: ssh <host> -C blowfish-cbc 'tar -cz /<folder>/<subfolder>' | tar -xvz to profile the different ssh ciphers on your machine, run this command: openssl speed aes rc4 blowfish At least on debian/ubuntu, the openssl command comes with the 'openssl' package, which is probably available by default on most systems.
bwoodacre · 470 weeks and 1 day ago
Nice comment, thx for the additions
polaco · 470 weeks and 1 day ago
You can let ssh do the compression/decompression: ssh -C HOST 'tar -c PATH' | tar -xv The advantage is that you can control the compression level, like: ssh -C -o CompressionLevel=9 HOST 'tar -c PATH' | tar -xv
rbossy · 470 weeks ago
Did some testes with the 3 versions of the commands (copying large log files) time scp hula2:/export/logs/account-collector/server.log.2009-11-* . server.log.2009-11-01 100% 43MB 8.5MB/s 00:05 server.log.2009-11-02 100% 44MB 10.9MB/s 00:04 server.log.2009-11-03 100% 21MB 10.5MB/s 00:02 server.log.2009-11-04 100% 2302KB 2.3MB/s 00:00 server.log.2009-11-05 100% 785MB 9.7MB/s 01:21 server.log.2009-11-06 100% 50MB 8.3MB/s 00:06 server.log.2009-11-07 100% 21MB 10.4MB/s 00:02 server.log.2009-11-08 100% 20MB 10.2MB/s 00:02 server.log.2009-11-09 100% 5171KB 5.1MB/s 00:01 server.log.2009-11-10 100% 343MB 8.8MB/s 00:39 server.log.2009-11-11 100% 109MB 9.1MB/s 00:12 server.log.2009-11-12 100% 109MB 8.4MB/s 00:13 real 2m46.731s user 0m25.078s sys 0m10.113s time ssh hula2 'tar -cz /export/logs/account-collector/server.log.2009-11-*' | tar -xvz tar: Removing leading `/' from member names export/logs/account-collector/server.log.2009-11-01 export/logs/account-collector/server.log.2009-11-02 export/logs/account-collector/server.log.2009-11-03 export/logs/account-collector/server.log.2009-11-04 export/logs/account-collector/server.log.2009-11-05 export/logs/account-collector/server.log.2009-11-06 export/logs/account-collector/server.log.2009-11-07 export/logs/account-collector/server.log.2009-11-08 export/logs/account-collector/server.log.2009-11-09 export/logs/account-collector/server.log.2009-11-10 export/logs/account-collector/server.log.2009-11-11 export/logs/account-collector/server.log.2009-11-12 real 1m8.055s user 0m7.232s sys 0m5.428s time ssh -C hula2 'tar -c /export/logs/account-collector/server.log.2009-11-*' | tar -xv tar: Removing leading `/' from member names export/logs/account-collector/server.log.2009-11-01 export/logs/account-collector/server.log.2009-11-02 export/logs/account-collector/server.log.2009-11-03 export/logs/account-collector/server.log.2009-11-04 export/logs/account-collector/server.log.2009-11-05 export/logs/account-collector/server.log.2009-11-06 export/logs/account-collector/server.log.2009-11-07 export/logs/account-collector/server.log.2009-11-08 export/logs/account-collector/server.log.2009-11-09 export/logs/account-collector/server.log.2009-11-10 export/logs/account-collector/server.log.2009-11-11 export/logs/account-collector/server.log.2009-11-12 real 1m28.785s user 0m13.613s sys 0m6.332s The ssh -C option is a good alternative but it probably compresses by packet, achieving a lower compression when compared to compression by file. Still the -C flag can be very useful. Thx for the additions guys.
polaco · 470 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: