The scp command
The scp command copies files between machines over ssh: scp notes.txt web…
The scp command copies files between machines over ssh: scp notes.txt web: copies a file to your home folder on web, and scp web:file . copies one back.
Taught in Intermediate, lesson 31
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
scp [-r] SOURCE... [USER@]HOST:PATH scp [USER@]HOST:PATH DEST
Copy files to or from another machine. Copies files over ssh. The side with HOST: is on the other machine; a path there without a / starts in your home folder. -r copies folders. It logs in the same way ssh does. In this practice shell the network is a simulation inside your browser: the machines web, db and router and the website example.com are pretend, and nothing leaves your device.
In the practice terminal:A simulation: web, db, router and example.com are pretend machines inside your browser, and nothing leaves your device. Files are copied between the pretend machines’ own folders.
How to use scp
Copy a file to a server
The colon marks the other machine. Nothing after it means your home folder there; give a path to put the file somewhere else.
scp notes.txt web:The authenticity of host 'web (10.0.0.10)' can't be established. ED25519 key fingerprint is SHA256:aaVFThjlH4/WIvuW5JbeDsUSex1ZSOHtUS3pZXDPB10. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'web' (ED25519) to the list of known hosts. learner@web's password: notes.txt 100% 361 0.5KB/s 00:00
scp report.txt learner@web:/tmp/The authenticity of host 'web (10.0.0.10)' can't be established. ED25519 key fingerprint is SHA256:aaVFThjlH4/WIvuW5JbeDsUSex1ZSOHtUS3pZXDPB10. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'web' (ED25519) to the list of known hosts. learner@web's password: report.txt 100% 35 0.1KB/s 00:00
Copy a file from a server
Put the remote side first and the local destination last; . is the folder you are in.
scp web:/etc/hostname . && cat hostnameThe authenticity of host 'web (10.0.0.10)' can't be established. ED25519 key fingerprint is SHA256:aaVFThjlH4/WIvuW5JbeDsUSex1ZSOHtUS3pZXDPB10. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'web' (ED25519) to the list of known hosts. learner@web's password: hostname 100% 4 0.1KB/s 00:00 web
Copy a folder: scp -r
scp -r projects web:The authenticity of host 'web (10.0.0.10)' can't be established. ED25519 key fingerprint is SHA256:aaVFThjlH4/WIvuW5JbeDsUSex1ZSOHtUS3pZXDPB10. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'web' (ED25519) to the list of known hosts. learner@web's password: main.js 100% 38 0.1KB/s 00:00 settings.yml 100% 13 0.1KB/s 00:00 ideas.txt 100% 17 0.1KB/s 00:00
Check that it arrived
scp notes.txt web: && ssh web lsThe authenticity of host 'web (10.0.0.10)' can't be established. ED25519 key fingerprint is SHA256:aaVFThjlH4/WIvuW5JbeDsUSex1ZSOHtUS3pZXDPB10. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'web' (ED25519) to the list of known hosts. learner@web's password: notes.txt 100% 361 0.5KB/s 00:00 learner@web's password: notes.txt README.txt
Options
| -r | copy folders and what is in them |
|---|---|
| -P PORT | another port (capital P) |
| -q | no progress line |
More examples
echo hi > note.txtscp note.txt web:The authenticity of host 'web (10.0.0.10)' can't be established. ED25519 key fingerprint is SHA256:aaVFThjlH4/WIvuW5JbeDsUSex1ZSOHtUS3pZXDPB10. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'web' (ED25519) to the list of known hosts. learner@web's password: note.txt 100% 3 0.1KB/s 00:00
scp web:/etc/hostname .learner@web's password: hostname 100% 4 0.1KB/s 00:00
Questions about scp
What is the difference between scp and rsync?
scp copies everything every time. rsync copies only what changed and can pick up where it stopped, which is better for big or repeated copies. Both work over ssh.
Why does scp say "No such file or directory"?
The file isn’t where you said, on this machine or the other one. A remote path without a leading / starts in your home folder there.
How do I use another port with scp?
With a capital P: scp -P 2222 file host: (ssh uses a small -p).
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.