Search code examples
linuxsshrsynctarscp

Elegantly send local tarball and untar on remote end


All,

This might be a FAQ, but I can't get my search-fu to find it. Namely, I kind of want to do the "reverse" tar pipe. Usually a tar pipe used to send a local folder to a remote location as a tar ball in a single nice command:

tar zcvf - ~/MyFolder | ssh user@remote "cat > ~/backup/MyFolder.tar.gz"

(I hope I got that right. I typed it from memory.)

I'm wondering about the reverse situation. Let's say I locally have a tarball of a large directory and what I want to do is copy it (rsync? scp?) to a remote machine where it will live as the expanded file, i.e.,:

Local:  sourcecode.tar.gz ==> send to Remote and untar ==>
Remote: sourcecode/

I want to do this because the "local" disk has inode pressure so keeping a single bigger file is better than many smaller files. But the remote system is one with negligible inode pressure, and it would be preferable to keep it as an expanded directory.

Now, I can think of various ways to do this with &&-command chaining and the like, but I figure there must be a way to do this with tar-pipes and rsync or ssh/scp that I am just not seeing.


Solution

  • You're most of the way there:

    ssh user@remote "tar -C /parent/directory -xz -f-" < sourcecode.tar.gz
    

    Where -f- tells tar to extract from stdin, and the -C flag changes directory before untarring.