move a lot of files over ssh

tar -cf - /home/user/test | gzip -c | ssh user@sshServer 'cd /tmp; tar xfz -'
Useful to move many files (thousands or millions files) over ssh. Faster than scp because this way you save a lot of tcp connection establishments (syn/ack packets). If using a fast lan (I have just tested gigabyte ethernet) it is faster to not compress the data so the command would be: tar -cf - /home/user/test | ssh user@sshServer 'cd /tmp; tar xf -'

By: esplinter
2009-08-24 18:35:38

2 Alternatives + Submit Alt

What Others Think

tar -czf and you spare that gzip pipe
lv4tech · 670 weeks and 3 days ago
why not try a nc? Here, I am assuming that you have shells open in both the systems. The netcat manual has an example which does just that. @lv4tech : i believe that tar internally does the same, pipes it to gzip if its -z, and bzip2 if its -j.
ninadsp · 670 weeks and 3 days ago
You could also have ssh use a different (maybe less "secure") cipher like Blowfish or RC4: tar -zcf - dirname | ssh -C blowfish-cbc user@host "cd /tmp; tar xfz -" see the different speeds yourself: openssl speed aes rc4 blowfish It'd be interesting to see how nc does versus ssh -C with blowfish or rc4/arcfour.
bwoodacre · 670 weeks and 3 days ago
Is this really saving tcp connection establishment? I tried this with many mp3 files and what I observed was, as soon as I start running it, I start seeing mp3 files in destination. So, it looks like, as if, tar is archiving/compressing single mp3, sending it through ssh and then single archive file is uncompressed at destination. So, my question is, in this case, does tar archive files one by one or compress them into single archive?
alperyilmaz · 635 weeks ago
the tar xfz on the ssh side should be zxf
elilindner · 471 weeks and 2 days ago
@alperyilmaz: Yes, tar creates a stream of files. Think if it as a multi-file cat command. As the stream is sent over the wire it is one TCP connection, The tar command would then write those files as the stream comes across there by looking like it is file by file.
Sukima · 411 weeks and 3 days 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? 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.


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: