Copying Between Computers: scp and rsync
scp copies a file or two to or from another computer, and rsync sends a whole folder but only what changed; both write the other computer's side as host:path, with a colon, and rsync -n rehearses first.
Journeyman · Unix · Linux · Updated
After this lesson you can
- Copy a file to or from another computer with scp, writing the other side as host:path
- Send a folder to another computer with rsync -av over SSH, rehearsing with -n first
Key takeaways
- host:path is a file on another computer. The colon is the border, and a path after it starts in your home folder there.
- scp FILE host:path sends a file and scp host:path . brings one back; -r copies a folder. Without the colon, scp copies on your own computer.
- rsync -av site/ host:folder/ sends a folder over SSH, and after the first time only what changed. The slash after site means "what's inside it".
- Add -n to rehearse and read the list before anything is copied.
Sam fixed the meetings page on the laptop. Now it has to get to the book club's server. You could log in with ssh and type the page again in nano, but the file is already written: it needs copying, not retyping. Two commands copy over SSH, with the same keys and short names: scp for a file or two, and rsync for a whole folder. The same jobs come up at work every week: putting a website live, fetching a log file from a server, sending a folder of data to a bigger computer.
The book club's site
The examples use a small practice site, and the short name bookclub from SSH Keys and ~/.ssh/config. Its server is your own computer, so "the server's" copies land in your home folder, and the output shown in this lesson is what these commands print with bookclub pointing at localhost. Make the site:
mkdir -p ~/terminal-practice/book-club/site/images && cd ~/terminal-practice/book-club
printf '<h1>Maple Street Book Club</h1>\n<p>We meet on the first Tuesday of every month.</p>\n' > site/index.html
printf '<h1>Meetings</h1>\n<p>December: The Hobbit</p>\n' > site/meetings.html
printf 'The Hobbit\nLittle Women\nFrankenstein\n' > site/reading-list.txt
touch site/images/cover.jpg
ls site
images index.html meetings.html reading-list.txt
host:path, the other computer's side
To name a file on another computer, write the computer, a colon and the path: bookclub:bookclub-site/index.html, read "on bookclub, the file bookclub-site/index.html". This is a remote path. A path after the colon starts in your home folder there; one that starts with / is absolute, such as bookclub:/var/www. bookclub: with nothing after the colon is your home folder there.
The colon is the whole difference. Leave it out, and the copy never leaves your computer:
scp site/index.html bookclub
ls
bookclub site
No error, no warning: scp made a local file named bookclub. Delete it with rm bookclub, and look for the colon whenever a copy "worked" but never arrived.
scp: a file there, a file back
scp (secure copy) works like cp, from and to, with one side on another computer:
scp site/meetings.html bookclub: # send a file to your home folder there
scp bookclub:bookclub-site/reading-list.txt . # bring one back into this folder
scp -r site bookclub:site-copy # -r: a whole folder
Each file shows a progress line while it travels. scp copies everything you name, every time, so for a folder you send again and again, use rsync.
rsync: a whole folder, only what changed
The rsync from Backups That Work: rsync copies between computers too. Put a host and a colon in front of one side, and it goes over SSH. -a keeps folders, dates and permissions, and -v lists each file. Rehearse first: -n (a dry run) lists what would be copied and copies nothing:
rsync -avn site/ bookclub:bookclub-site/
sending incremental file list
created directory bookclub-site
./
index.html
meetings.html
reading-list.txt
images/
images/cover.jpg
sent 209 bytes received 71 bytes 560.00 bytes/sec
total size is 167 speedup is 0.60 (DRY RUN)
The list is right, so send it for real: the same command without -n. Then add a line to one page and send the folder again:
rsync -av site/ bookclub:bookclub-site/
printf '<p>January: Little Women</p>\n' >> site/meetings.html
rsync -av site/ bookclub:bookclub-site/
sending incremental file list
created directory bookclub-site
./
index.html
meetings.html
reading-list.txt
images/
images/cover.jpg
sent 540 bytes received 143 bytes 1,366.00 bytes/sec
total size is 167 speedup is 0.24
sending incremental file list
meetings.html
sent 317 bytes received 42 bytes 718.00 bytes/sec
total size is 196 speedup is 0.55
The second time, only meetings.html traveled. Your byte counts may differ a little. For a site with hundreds of photos, that's the difference between seconds and minutes.
The slash after site
site/ with a slash means "what's inside site"; site without one means "the folder site itself". On your own computer, side by side:
rsync -a site/ backup
rsync -a site backup2
ls backup backup2
backup:
images index.html meetings.html reading-list.txt
backup2:
site
Across computers it's the same: rsync -av site bookclub:bookclub-site/ puts the pages in bookclub-site/site/, where the web server won't look. Delete the two practice copies with rm -r backup backup2.
Check yourself
2 questions to try on the lesson page.
So what
scp sends a file and rsync sends a folder, both with the other computer written as host:path. Rehearse with -n, mind the slash, and the server gets exactly what you meant.