Create a local compressed tarball from remote host directory

ssh user@host "tar -cf - /path/to/dir" | gzip > dir.tar.gz
The command uses ssh(1) to get to a remote host, uses tar(1) to archive a remote directory, prints the result to STDOUT, which is piped to gzip(1) to compress to a local file. In other words, we are archiving and compressing a remote directory to our local box.

By: atoponce
2011-12-14 15:54:57

3 Alternatives + Submit Alt

What Others Think

Useful +1
zlemini · 555 weeks and 6 days ago
I needed to add some redirection after the pipe: | gzip > dir.tar.gz
zlemini · 555 weeks and 6 days ago
@zlemini Ah, yes. Simple typo, and fixed. Thanks!
atoponce · 555 weeks and 6 days ago
Better to do the compression on the remote side; use 'tar czf' over there and just write it straight to a file here.
Mozai · 555 weeks and 5 days ago
@Mozai I wouldn't necessarily say that doing compression remotely is "better", but it's certainly an alternate way of doing the command. If you're concerned about data bandwidth on the wire, OpenSSH (and many other SSH implementations) support compression to varying degrees. Lastly, not all tar(1) implementations support compression builtin. Best to keep the command 100% portable, as much as possible, than relying on platform-specific enhancements.
atoponce · 555 weeks and 5 days ago
There may be times where you have plenty of bandwidth but very short of remote processor so this could be useful in that case. I'd say it's usually better to do the compression on the remote side, though. You certainly wouldn't want to use ssh compression, because then you'd be compressing, decompressing, and compressing again. If your tar doesn't have the z option, you would use this: ssh user@host "tar -cf - /path/to/dir | gzip" > dir.tar.gz Also, if you didn't want "/path/to/dir" in each archive filename, you could do: ssh user@host "cd /path/to/dir; tar -cf - . | gzip" > dir.tar.gz
splante · 553 weeks and 1 day ago
you can use rsh with tar and gzip ,,,this url will guide you with examples.....
parashuramsingh · 548 weeks and 2 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: