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 · 672 weeks and 1 day ago
Nice comment, thx for the additions
polaco · 672 weeks 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 · 671 weeks and 6 days 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 · 671 weeks and 6 days ago
Pug Puppies for Sale Near Me pugs puppies for sale teacup pugs for sale pug puppies for sale by owner pug puppies ohio PUG PUPPY FOR SALE NEAR ME PUG PUPPIES FOR SALE pug puppies for sale in kentucky Pug Puppies for Sale Under $500 Near Me pug puppies for sale in texas pug puppies for sale $200 pugs for sale near me under $500 pugs for sale under $400 near me pugs for sale near me puppies for sale near me under $500 pug puppies for sale under $1,000 near me pug for sale pug puppies for sale under $300 Brindle Pug Pitbull Pug Mix Pugs for sale cheap Cheap pug affordable pug puppies for sale near me black pugs for sale near me White Pugs for sale pug dog for sale free pug puppies pug puppies for sale in my area mn pug breeders pug puppies indiana pugs for sale michigan PUG PUPPY ADOPTION Pug puppies for sale Pug puppies for sale near me Pug puppies near me Pug Puppies for Sale Under $500 Near Me Cute Pug Puppies Black pug puppies Black pug puppies for sale pug puppies for adoption black pug puppies for sale near me chihuahua pug mix puppies how much is a pug puppy teacup pug puppies baby pug puppies pictures of pug puppies pug puppies for sale in Ohio pug puppies price pug mix puppies teacup pug puppies for sale best food for pug puppy newborn pug puppies pug puppies craigslist pug puppies for sale craigslist adorable pug puppies how much does a pug puppy cost Pitbull pug mix puppies pug pit mix puppy pug puppies for sale $200 pug puppies for sale in NJ Pug puppies for sale in Wisconsin pug puppy cost pug puppy food royal canin pug puppy royal canin pug puppy food fawn pug puppy pug puppies for sale florida pug puppies for sale in Indiana pug puppies for sale in KY pug puppies for sale in NC pug dog puppy AKC Registered Pug Puppies For sale cheap pug puppies for sale near me cheap pug puppies for sale in California cheap pug puppies for sale in nj Black Pug Puppies for sale pugs puppies for sale pug puppies indiana Amazing! This blog looks just like my old one! It's on a completely different subject but it has pretty much the same layout and design. Wonderful choice of colors!
rahimhh21 · 3 weeks and 6 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?

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: