in Technology

Transfer directories between computers using SSH and tar

When I need to transfer lots of files or directories between computers, I usually use tar and ssh together. Last time I used it however, I realized that perhaps not everyone knows how to do this.

The procedure is very simple, and a full command could look something like this:

tar -cf – directory/ | ssh my.other.computer tar -xf – -C /destination/

Simple huh? If you want compression, just add z for gzip or j for bzip2 to both tar statements. This could be necessary if you are planning to do this over slow lines.

Comment

Comment

  1. Why not use rsync? It’s simplier and does the same thing. For your example:

    rsync -a directory my.other.computer:/

    User [email protected] if want to use another account. I believe all disto’s use ssh as transport but you make sure by typing it this way:

    rsync -a –rsh=ssh directory my.other.computer:/

    Use -z for compresssion.

  2. [quote comment=”29381″]Why not use rsync? It’s simplier and does the same thing.[/quote]
    Yes, this is a valid and good point.

    The reason however for me not using rsync is because it is not installed in the default Debian distribution, which I use on some servers. I generally don’t need it for anything else, so I don’t bother installing it.

    Great tip though, and thank you for your input!

  3. Rather than piping the output to ssh would it not be simpler to just scp it with a command like this: tar xvjf archive.tar.bz2 directory/ && scp archive.tar.bz2 [email protected]:/path/where/you/want/file

    This is the way I’d do it anyhow!

  4. [quote comment=”30580″]Rather than piping the output to ssh would it not be simpler to just scp it with a command like this: tar xvjf archive.tar.bz2 directory/ && scp archive.tar.bz2 [email protected]:/path/where/you/want/file

    This is the way I’d do it anyhow![/quote]
    I guess you mean tar cvjf, but yes, this is also quite possible. The thing is that this will take longer, since it is a five-step process; 1. pack the files, 2. transfer the files, 3. unpack the files, 4. delete the archive on the target.., 5. .. and on the source.

    Thanks for your input though. It’s great to see lots of possible solutions to the same problem.

  5. I’d make it

    tar -cSf – directory/ | ssh my.other.computer tar -xf – -C /destination/

    instead.

    Without the S you’ll get a disk use surprise at the receiving end if the directory you are transferring has sparce files.

  6. [quote comment=”60621″]I’d make it

    tar -cSf – directory/ | ssh my.other.computer tar -xf – -C /destination/

    instead.

    Without the S you’ll get a disk use surprise at the receiving end if the directory you are transferring has sparce files.[/quote]
    You are correct, thanks!