Piping Tar over SSH
Oct 25

I recently had the issue where I wanted to Tar/Gzip up a large directory but didn’t have the available space on the server, but I need to get it transfered over to another server.  So I came up with two options.  First, Tar/Gzip it and pipe it over SSH and create the tgz file on the remote host, or just pipe and extract it directly on the other host.

The first option, creating the tgz on the remote host can be accomplished by doing this

cd /my/path
tar czf – . | ssh remoteserver “cat > /new/path/file.tar.gz”

The second option, creating the tgz but extracting the contents directly on the remote host can be accomplished by doing this.

cd /my/path
tar czf – . | ssh remoteserver “cd /remote/path; tar xzf -”

Another thing you can do is if you want a path on a remote server but you want to pull it to the local server and extract it you can run the following

cd /my/path
ssh remote “cd /my/path; tar czf – .” | tar xfz -


Switch to our mobile site