How do I copy files to and from a server over SSH?

Use scp notes.txt user@host:/tmp/ to upload and scp user@host:/var/log/app.log . to download. Add -r for a folder and -P for a port other than 22. For a transfer you may need to repeat, use rsync.

Last updated

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), .

scp and sftp use the same SSH login as ssh: the same keys, the same host keys and the same ~/.ssh/config. To copy a file, write the source, then the target, and put a colon after the host name for the side that is on the server. The examples below use the alias app from the config file article, so there is no user name or address to type.

Upload one file

$ scp -q notes.txt app:/tmp/notes.txt
$ ssh app 'wc -c /tmp/notes.txt'
34 /tmp/notes.txt

The colon is what makes app a server. Without it, scp copies to a local file named app. -q hides the progress meter, so the output stays short here. Without -q you see a line with the percentage and the speed. The second command checks that the 34 bytes arrived. For a file that matters, compare checksums on both ends. The two hashes must be identical:

$ sha256sum notes.txt
11c420c38ad940436cff156ccb9dba14f9ad9a046af35aa5ac3384ac5d81cbdf  notes.txt
$ ssh app 'sha256sum /tmp/notes.txt'
11c420c38ad940436cff156ccb9dba14f9ad9a046af35aa5ac3384ac5d81cbdf  /tmp/notes.txt

A size can match while the content does not, for example after a transfer that was cut. A checksum cannot. sha256sum prints the hash of the file, and a single changed byte gives a different one.

Download a file, or several

$ scp -q app:/var/log/app/app.log .
$ head -n 2 app.log
GET /index.html 200
GET /about.html 404

The dot is the current folder. A pattern on the server side goes in quotes, so that your shell does not try to expand it first:

$ scp -q 'app:/var/log/app/*.log' logs/
$ ls logs
app.log  worker.log

Copy a folder with -r

-r copies a folder and everything in it. Upload one:

$ scp -rq site app:/tmp/
$ ssh app 'find /tmp/site | sort'
/tmp/site
/tmp/site/css
/tmp/site/css/app.css
/tmp/site/index.html

And download one. The folder lands in the target folder under its own name:

$ scp -rq app:/var/www/html .
$ find html | sort
html
html/about.html
html/img
html/img/logo.txt
html/index.html

scp copies everything each time and overwrites files of the same name without asking. If you copy the same folder again and again, switch to rsync, which sends only what changed and can show you the changes first.

A different port

For ssh the port option is -p. For scp it is -P, with a capital letter, because scp -p already means “keep times and modes”. A wrong port looks like this:

$ scp -P 2200 notes.txt app:/tmp/
ssh: connect to host app.example.test port 2200: Connection refused
scp: Connection closed

Nothing listens on port 2200 there. This server also runs sshd on port 2222:

$ scp -q -P 2222 notes.txt app:/tmp/notes-2222.txt
$ ssh -p 2222 app 'ls /tmp/notes-2222.txt'
/tmp/notes-2222.txt

If a server always uses port 2222, write Port 2222 in its block of ~/.ssh/config, and no tool needs the option again. For a server that is reachable only through a bastion, scp -J bastion file target:/path takes the same jump host option as ssh. The jump host article has the details.

sftp: look first, then copy

sftp is a small interactive client. Run sftp app and you get a prompt where ls, cd, get, put, mkdir and bye work on the server, and lcd changes your local folder. For a script, or to see a whole session at once, give it commands on standard input with -b -:

$ printf 'cd /var/www/html\nls -1\nget index.html\n' | sftp -q -b - app
sftp> cd /var/www/html
sftp> ls -1
about.html
img
index.html
sftp> get index.html
$ cat index.html
<h1>Production</h1>

get downloaded the file into the current folder. Use sftp when you do not know the exact name yet or when a server accepts SFTP and nothing else.

Mistakes people make

You forget the colon, and scp creates a local file named like the host. Check where the colon sits.

You use -p for the port. For scp the port is -P.

You let your shell expand a pattern meant for the server. Put the whole host:path in single quotes.

You overwrite a file by accident. scp does not ask. Copy to a new name first when the target may already exist.

You copy files that arrive with the wrong owner or mode. The permissions article shows how to fix a tree.

The Files panel in Valakes moves files between your computer and the server of the open tab over SFTP, on the connection you already have, so there is no second login. A transfer that is cut in the middle starts that file again with Retry; Valakes cannot resume it. Feature: Files

Questions

Should I use scp, sftp or rsync?

Use scp for one file or one folder, once. Use sftp when you want to look around the server first. Use rsync for a folder you copy again and again, a large transfer that may be cut, or anything where you want to see the changes before they happen.

Does scp use the SCP protocol?

Not on Ubuntu 24.04. Its scp(1) says scp transfers data with the SFTP protocol over an ssh connection. The option -O selects the legacy SCP protocol, which you may need for a server that has no SFTP.

How do I copy from one server to another?

Name both ends, as in scp one:/path/file two:/path/. The data goes through your computer by default. The option -R makes the first server connect to the second itself, and that needs a login from the first to the second without a password.

Does scp keep file permissions and times?

Only with -p. Without it, the copy gets the modification time of the moment you copied it.

References

The app is free.

Get notified at launch

macOS is coming to the Mac App Store. iOS and Android later.