Part III · Shell and commands
29.5 Copying files over SSH: scp, sftp and rsync
alsoscpsftpsshfscopy over sshcopy to serveruploaddownload file
TL;DR
- Everything that copies over SSH uses the same keys and the same
~/.ssh/confignames: ifssh dbworks,scp file db:works. scpfor a quick copy,rsyncfor folders and anything big (it resumes and only sends differences),sshfsto mount a remote folder.- A path without a leading
/afterhost:starts in your home folder there.
scp report.pdf db:/tmp/
scp -r db:logs/ ./logs-db/
rsync -avP project/ db:project/ # resumable, shows progress
Pitfalls
rsync src/ dstandrsync src dstdiffer: the trailing slash copies the folder's contents, not the folder (28.7).- Interrupted
scpstarts over;rsync -Ppicks up where it stopped. - Remote paths with spaces need quoting twice with old scp: once for your shell, once for the remote one. Recent OpenSSH no longer needs it.
- sshfs over a slow link makes every
lsslow: copy what you work on.
Deep dive 2 questions
What changed in scp?
The old scp protocol ran a shell command on the server and trusted its
output, which caused security bugs and odd quoting. Since OpenSSH 9.0,
scp speaks the SFTP protocol underneath, with the same command line.
scp -O brings back the old protocol for servers without SFTP.
Why is rsync faster on the second run?
It compares sizes and dates, and for changed files, checksums of blocks, then sends only the blocks that differ. Copying a 10 GiB folder again after a few edits sends a few megabytes.