Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to essentially tar/gz a directory on a remote machine and save the file to my local computer without having to connect back into my local machine from the remote one. Is there a way to do this over SSH? The tar file doesn't need to be stored on the remote machine, only on the local machine. Is this possible?

share|improve this question
up vote 12 down vote accepted

You can do it with an ssh command, just tell tar to create the archive on its standard output:

ssh remote.example.com 'cd /path/to/directory && tar -cf - foo | gzip -9' >foo.tgz

Another approach, which is more convenient if you want to do a lot of file manipulations on the other machine but is overkill for a one-shot archive creation, is to mount the remote machine's filesystem with SSHFS (a FUSE filesystem). You should enable compression at the SSH level.

mkdir ~/net/remote.example.com
sshfs -C remote.example.com:/ ~/net/remote.example.com
tar -czf foo.tgz -C ~/net/remote.example.com/path/to/directory foo
share|improve this answer
I'm just curious, why'd you recommend tar -cf - foo | gzip -9 instead of tar -czf - foo or tar -cz foo? – Patrick Jul 17 '13 at 12:23
@Patrick Slightly more portable, e.g. it'll work on Solaris. – Gilles Jul 17 '13 at 13:20
Would you be so kind to describe all arguments? – Rootical V. May 8 at 13:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.