scp, SFTP and rsync
Level 4 · Moving files
Three ways to move files over SSH. scp copies once, SFTP opens a session you can browse, and rsync sends only what changed. When to use each.
Course contentsLesson 7 of 9
Level 1 · Connecting
Level 2 · Keys and shortcuts
Level 3 · Locking the door
Level 4 · Moving files
Level 5 · Reaching further
After this lesson you can
- Copy a file with scp
- Browse and upload with sftp
- Sync a folder with rsync
13 mincovers 3 glossary entries
Worth reading first: sshd_config and Fail2ban
Which tool to use to move files
Three tools move files over SSH, and all of them use the same keys and the same locked line. scp copies files in one command, like cp across machines. SFTP opens a session where you can list folders, upload and download. rsync compares both sides and sends only what changed. For one file, use scp. For a folder you update often, use rsync.
scp reads like a copy command with an address on one side. scp report.pdf emma@203.0.113.10:/home/emma/ sends one file up. Swap the two sides to bring a file down. Add -r to copy a whole folder.
SFTP is a way to browse files on a server, built into SSH. sftp jack@203.0.113.10 opens a session. Inside it, ls lists files, put uploads one and get downloads one. Many file manager apps speak SFTP too, so you can drag and drop.
rsync is built for folders that change. The first run copies everything. After that, it compares sizes and dates on both sides and sends only the files that changed. A site of 2,000 files with 3 edits sends 3 files.
rsync -av site/ lucy@203.0.113.10:/var/www/site/ is the usual form. -a keeps dates and rights, and -v lists each file. Adding -n first shows what would be sent, with nothing sent at all.
Watch the slash at the end of the source. site/ copies what is inside the folder. site, with no slash, copies the folder itself, so you end up with /var/www/site/site.
A common mistake is rsync --delete without a dry run. It removes files on the far side that are not on yours. With the wrong path, that can be every file on the server. Run it with -n first and read the list.
Which one to pick
Worked example
The words for this lesson3
Check yourself
You update the same 2,000-file website folder every day. Which tool fits best?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.