Copy over ssh

How to copy a file or entire directory over ssh, archived or not. Using ssh only, in case scp or sftp aren’t available.

Directory, Local -> Remote

tar -zcvf - /path/to/dir/* | ssh user@remote 'cd DESTDIR ; tar -zxvf -'
tar -zcvf - /path/to/dir/* | ssh user@remote "cat /path/to/remote/file.tar.gz"

The “-” after the “f” in tar redirects, so first tar archives the directory to stdout which gets piped to the other side and becomes input for the second tar. “z” can be replaced with “j” for better compression if both tar know about it.

Directory, Remote -> Local

ssh user@remote "tar -jcvf - /path/to/dir/*" | tar -zxvf -
ssh user@remote "tar -jcvf - /path/to/dir/*" | cat > localarchive.tar.bz2

File, Local -> Remote

cat /path/to/file | ssh user@host cat ">" /path/to/file
dd if=/path/to/file | ssh user@host dd of=/path/to/file

File Remote -> Local

ssh user@host cat /path/to/remote > /path/to/local
ssh user@host dd if=/path/to/remote | dd of=/path/to/local

Directory/File Remote1 -> Remote2

ssh user@host1 "tar -cvf - /path/to/source/files*" | ssh user@host2 "cd /path/to/destination; tar -xvf -"