scp examples: copy files and folders over SSH

To upload a file, run scp file.txt user@server:/tmp/. To download one, run scp user@server:/tmp/file.txt .. Add -r to copy a folder and -P 2222 for another port, with a capital P. A remote path needs the colon, because without it scp treats the name as a local file.

Updated . OpenSSH 10.3 scp on macOS to OpenSSH 10.0 on Debian 13 in a throwaway container.

Common examples

TaskCommand
Upload a filescp report.pdf user@server:/tmp/
Download a filescp user@server:/tmp/report.pdf .
Download with a new namescp user@server:/tmp/report.pdf copy.pdf
Upload a folderscp -r site user@server:/tmp/
Download what is in a folderscp -r 'user@server:/tmp/site/*' dl/
Another portscp -P 2222 file user@server:/tmp/
Use a specific keyscp -i ~/.ssh/id_work file user@server:/tmp/
Keep times and modesscp -p file user@server:/tmp/
Limit speed, in Kbit/sscp -l 800 file user@server:/tmp/
Compressscp -C file user@server:/tmp/
Through a jump hostscp -J user@jump file user@server:/tmp/

A Host entry in ~/.ssh/config also works here: scp file server:/tmp/. See the SSH config file.

The -J row goes through a jump host. To save that route for good, see ProxyJump and jump hosts.

Copying a folder twice nests it

If the destination folder already exists, scp -r puts the folder inside it. With /tmp/site already on the server, this gave us /tmp/site/site:

scp -r site user@server:/tmp/site

So a first run creates /tmp/site, and a second creates /tmp/site/site. To copy a folder repeatedly, use rsync instead.

Names with spaces

scp "my file.txt" "user@server:/tmp/my file.txt"

This works with the default SFTP protocol. With the old protocol from -O, the same line failed with scp: ambiguous target, because the remote side reads the name again. In that case, add a second pair of quotes:

scp -O "my file.txt" 'user@server:"/tmp/my file.txt"'

Between two servers

scp -3 user@one:/tmp/file user@two:/tmp/

-3 sends the data through your machine, so neither server needs a login on the other. Since OpenSSH 8.7 this is the default, so the flag only makes it explicit. In our test, a copy without -3 also ran through the local client.

To have the first server connect directly to the second, use -R:

scp -R user@one:/tmp/file user@two:/tmp/

The first server then has to log in to the second without a prompt, and it must already know its host key. Otherwise you get Host key verification failed., which is what happened in our test.

-O: the legacy protocol

Since OpenSSH 9.0, scp uses the SFTP protocol by default. You can see this with scp -v, which prints Sending subsystem: sftp. With -O it prints Sending command: scp -v -t /tmp/.

scp -O file user@server:/tmp/

You need -O when the other side has no SFTP subsystem, such as some embedded devices. In that case, the usual error is subsystem request failed on channel 0. See that error's guide.

What scp does not do

  • It does not continue a broken transfer. Run it again and it starts over.
  • It does not send only what changed.

For those, use rsync, or reget in sftp. For a full comparison, read scp vs SFTP vs rsync.

Questions

How do I copy a folder with scp?

Add -r: scp -r folder user@server:/tmp/. If the destination folder already exists, scp puts the folder inside it.

How do I use scp with a different port?

Use a capital -P, as in scp -P 2222 file user@server:/tmp/. A lowercase -p keeps file times and modes instead.

Can scp resume an interrupted transfer?

No. Running it again starts the file over. Use rsync with --partial, or reget and reput inside sftp.

Sources

More in SFTP and file transfer

All guides