Tar over ssh

Just a very short note. It’s not so difficult, but I keep on forgetting the syntax. Here it is:

me@localhost$ tar cf - . | \
    ssh user@otherhost 'cd dir; tar xf -'

5 Responses to “Tar over ssh”

  1. Marc says:

    Why not just use scp -pr? E.g.: scp -pr . user@otherhost:dir/

    If you need to do what you mentioned though and keep forgetting, why not create a function – e.g.:

    tarssh()
    {
    tar cf – . | ssh $1 “cd $2 && tar xf -”
    }

    I didn’t test this, but something like this should work.

    Note also that I changed a ; to a && so that it won’t untar into your home directory if it can’t cd into the directory.

  2. Bram says:

    I actually had a tar file that I wanted to extract in multiple home directories (some temporary guest accounts)…and just like to chain tools together with pipes ;-)

  3. dirk says:

    Tar also has a function to do ‘chdir’ for you, so you can invoke as
    me@localhost $ tar cf – . | ssh user@otherhost tar -C dir -xf -
    (note that you also do not need quotes for this)

    I usually also add a ‘z’ to both tars, to use compression. Most pcs can decompress so fast that network throughput is still the bottleneck, even with compression.

  4. uidzero says:

    Dirk,

    Thanks, just what I was looking for. :)

    uidzero

  5. netlest says:

    scp is sometimes not good becouse it can keep only file permisions without original file owners. tar can :)

Leave a Reply