Table of Contents

SSH Copy and Sync

SCP (Secure Copy) and rsync transfer files over SSH. SCP is simpler for one-time copies; rsync is better for syncing because it skips unchanged files and can resume interrupted transfers.

SCP basics:

$ scp file user@host:/path          # copy file to remote
$ scp user@host:/path/file .        # copy from remote
$ scp -r dir user@host:/path        # copy directory recursively
$ scp -P 2222 file user@host:/path  # use non-standard port (note: -P not -p)

Preserve permissions and timestamps:

$ scp -p file user@host:/path

Copy between two remotes:

$ scp -3 user1@host1:/path user2@host2:/path

Rsync is more powerful (delta transfer, resume, delete):

$ rsync -av -e ssh dir user@host:/path
$ rsync -av --delete -e ssh dir user@host:/path  # also delete files on remote
$ rsync -av --partial -e ssh dir user@host:/path # keep partial files for resume

Common rsync options:

-a       archive (recurse, preserve permissions/times)
-v       verbose
-z       compress
-P       show progress and keep partial files
--delete delete files on dest not on source
--exclude pattern  skip matching files
-e "ssh -p 2222" use specific SSH port

Rsync for syncing directories:

$ rsync -av -e ssh ~/mydir user@host:~/backup/

Subsequent syncs only transfer changed files, making rsync much faster for repeated syncs.

Dry run (show what would be copied):

$ rsync -av --dry-run -e ssh dir user@host:/path

Both SCP and rsync encrypt traffic via SSH.