Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

scp: copy files and folders over SSH

By updated

Improve this page

You want to send a configuration to a server, or bring back a log to analyse it at home. scp does that copy in one line, with the same syntax as cp: the source first, the destination second. It goes through SSH, so the transfer is encrypted and uses your keys and your ~/.ssh/config. Two surprises wait for beginners: a folder is refused without -r, and a file already on the server is overwritten without any question.

This page teaches these steps on a practice folder, checking the result on the server each time. For a repeated synchronisation, see rsync; for an interactive session, sftp.

What you will learn

  • Set up a practice SSH server and connect to it with a key
  • Send a file to a server and bring one back
  • Copy a folder with -r and predict where it arrives
  • Keep the date and the permissions of a file with -p, without mixing up -p and -P, the port
  • Know that scp overwrites without asking
  • Read the scp error messages and choose between scp, rsync, sftp and tar

Prerequisites

You know how to open a Linux terminal, move around with cd, list with ls and copy with cp. You can also change permissions with chmod (change permissions), search with find (find) and run a command on a server with ssh host 'command' (remote commands). Work with an ordinary account, not with root.

Prepare the practice server

scp needs an SSH server that accepts your key. If you do not have one, your own machine plays that role, with localhost as its address. Install the SSH server and start it (configure sshd details the service). Its name depends on the distribution: ssh on Debian and Ubuntu, sshd on AlmaLinux.

Debian, Ubuntu
sudo apt install openssh-server
sudo systemctl enable --now ssh
AlmaLinux
sudo dnf install openssh-server
sudo systemctl enable --now sshd

If you do not have a key yet, create one with ssh-keygen -t ed25519 (create an SSH key). Then give your server a name in ~/.ssh/config (the SSH client explains the file). Replace moi ("me") with your account, and localhost with the address of your server if you have one.

~/.ssh/config
Host srv
HostName localhost
User moi

Copy your public key to the server, then connect once to accept its fingerprint (answer yes, then type exit):

Terminal window
ssh-copy-id srv
ssh srv

If your key has a passphrase, load it once into the agent with ssh-add, otherwise every scp asks for it again. Finally, check that ssh srv true prints nothing and does not ask for a password. If this command fails, scp srv: will fail too.

Create the practice folder

The examples use ~/scp-lab locally and a single folder on the server, scp-lab-srv. It is created first: if it already exists, the command stops and nothing else is created. Everything the page sends goes into it, and the final cleanup only deletes it. touch -d gives the files a date in 2020 so that -p shows. outil.sh has permissions 770, gros.bin weighs 1 MB, and scp-lab-srv/a.txt contains ANCIEN on the server. The names use French words, which the commands keep: outil is "tool", gros "big", ANCIEN "old", NOUVEAU "new", projet "project", bonjour "hello", fin "end", and later recu "received", envoye "sent", copie "copy", contenu "content", sans "without", avec "with", cle "key", retour "return", AJOUT "addition", mon "my", espace "space" and illisible "unreadable".

Terminal window
ssh srv mkdir scp-lab-srv && mkdir ~/scp-lab && cd ~/scp-lab && {
mkdir -p projet/sub
printf 'bonjour\n' > projet/t.txt; printf 'fin\n' > projet/sub/f.txt
printf 'NOUVEAU\n' > a.txt; printf 'echo ok\n' > outil.sh; chmod 770 outil.sh
head -c 1000000 /dev/zero > gros.bin
touch -d '2020-01-02 03:04' projet/t.txt projet/sub/f.txt a.txt outil.sh
ssh srv 'printf "ANCIEN\n" > scp-lab-srv/a.txt'
}

The outputs on this page come from an OpenSSH 10.0p2 client (Debian 13) talking to an OpenSSH 10.2p1 server (Ubuntu); ssh -V prints your version. Stay in ~/scp-lab until "Clean up the practice folder". Remote paths without a leading / start from your home folder on the server.

The scp command in detail

An scp command is written in this order: the options, the source, then the destination. The srv: prefix designates the server; without it, the path is local. Read the line from left to right: each number points to the explanation of that piece.

Anatomy of the command

Syntaxscp [options] source destination

  1. scp the programIt copies over SSH.
  2. -r the directoriesoptionalEnter subdirectories.
  3. -p the datesoptionalKeep the date and permissions of the file.
  4. -P 22 the portoptionalThe SSH one, with a capital P.
  5. projet the sourceHere, on your machine.
  6. srv:dest/ the destinationThe server, a colon, then the path.

Outputnothing: the copy succeeds

The 3 usage scenarios

The direction of the copy depends on no option: it depends on the position of the srv: prefix. On the left, you bring a file back; on the right, you send one. First bring back a.txt from the server under the name recu.txt, then send your a.txt under the name envoye.txt.

Terminal window
scp srv:scp-lab-srv/a.txt recu.txt
cat recu.txt
scp a.txt srv:scp-lab-srv/envoye.txt
echo $?
ssh srv ls scp-lab-srv
Output
ANCIEN
0
a.txt
envoye.txt

recu.txt contains ANCIEN, the content of the file on the server. The upload prints nothing and returns code 0: a successful copy is silent. Always check on the server side, here with ssh srv ls, which shows envoye.txt next to a.txt.

Transfer between two remote servers

With two prefixes, scp copies from one server to another. The -3 option routes the data through your machine, which helps when the two servers cannot reach each other directly. It uses your bandwidth twice. Here both sides are srv, which is enough to see the syntax.

Terminal window
scp -3 srv:scp-lab-srv/a.txt srv:scp-lab-srv/via-local.txt

The command prints nothing and returns code 0. The OpenSSH manual says that going through your machine is the default behaviour: -3 makes it explicit, and -R asks for a direct copy between the two servers.

Forgetting -r for folders

scp refuses a folder without -r, and copies nothing. The message gives the cause on the first line and the failed operation on the second, and the return code is 1.

Terminal window
scp projet srv:scp-lab-srv/
echo $?
Output
scp: local "projet" is not a regular file
scp: failed to upload file projet to scp-lab-srv/
1

With -r, scp copies the folder and everything it contains. The folder itself arrives in the destination; check it with find on the server.

Terminal window
scp -r projet srv:scp-lab-srv/
ssh srv 'cd scp-lab-srv && find projet -type f | sort'
Output
projet/sub/f.txt
projet/t.txt

The result depends on the destination: if it does not exist, the copy takes that name; if it exists, the source arrives inside it as a subfolder. Running the same command again therefore changes the result. Run it twice and compare the two lists:

Terminal window
scp -r projet srv:scp-lab-srv/copie
ssh srv 'cd scp-lab-srv && find copie -type f | sort'
scp -r projet srv:scp-lab-srv/copie
ssh srv 'cd scp-lab-srv && find copie -type f | sort'
Output of the first find
copie/sub/f.txt
copie/t.txt
Output of the second find
copie/projet/sub/f.txt
copie/projet/t.txt
copie/sub/f.txt
copie/t.txt

On the second run, copie existed: projet arrived in it as a subfolder, and the files from the first run stayed. With -r, a trailing slash (projet/) changes nothing, unlike rsync. To send the content without the folder, the source is projet/*: the shell replaces it with projet/sub and projet/t.txt before scp runs, and scp receives them as two sources.

The content of a folder, hidden files included Exercise 1 of 2 · ~8 min · terminal

A colleague has to copy the whole content of projet to the server, hidden files included but without the projet folder itself, into scp-lab-srv/contenu/. They think they are done as soon as the command says nothing.

Prepare both sides

Add a hidden file .env containing secret to projet, and create the contenu folder in scp-lab-srv on the server.

Send the content like the colleague

Send the items of projet into contenu/, without the projet folder, using a shell wildcard.

Compare both sides

List projet locally and contenu on the server, hidden files included, and look for what is missing. Complete the copy, then compare again.

A hint 1 / 1

A shell wildcard does not match names that start with a dot. ls -A shows them.

I have tried: show the solution
Terminal window
printf 'secret\n' > projet/.env
ssh srv mkdir scp-lab-srv/contenu
scp -r projet/* srv:scp-lab-srv/contenu/
ls -A projet
ssh srv ls -A scp-lab-srv/contenu
scp projet/.env srv:scp-lab-srv/contenu/
ssh srv ls -A scp-lab-srv/contenu
ls -A projet, on your side
.env
sub
t.txt
ls -A contenu, before sending .env
sub
t.txt
ls -A contenu, after sending .env
.env
sub
t.txt

The shell expands projet/* without the hidden files: .env stays on your side, with no error message and with code 0. Only the comparison of the two ls -A reveals it. Sending projet/.env explicitly completes contenu, which then holds .env, sub and t.txt.

The -p vs -P trap

Both options exist and only differ by case. -P (uppercase) chooses the SSH Port. -p (lowercase) keeps the modification date and the permissions of the file. Without -p, the copy carries the current date. Check the dates on the server side (%TY prints the year of modification):

Terminal window
scp a.txt srv:scp-lab-srv/sans-p.txt
scp -p a.txt srv:scp-lab-srv/avec-p.txt
ssh srv 'cd scp-lab-srv && find sans-p.txt avec-p.txt -printf "%TY %p\n"'
Output
2026 sans-p.txt
2020 avec-p.txt

Look at the first column: the year of the copy for sans-p.txt, the original year, 2020, for avec-p.txt. Permissions follow the same logic. outil.sh has permissions 770; %m prints those of the copies in octal.

Terminal window
scp outil.sh srv:scp-lab-srv/sans-p.sh
scp -p outil.sh srv:scp-lab-srv/avec-p.sh
ssh srv 'cd scp-lab-srv && find sans-p.sh avec-p.sh -printf "%m %p\n"'
Output
750 sans-p.sh
770 avec-p.sh

Without -p, the permissions of the copy depend on the server: 750 here, instead of the original 770.

If you type -p 2222 thinking you give a port, scp reads 2222 as the name of a file to copy. The command fails, which limits the damage:

Terminal window
scp -p 2222 a.txt srv:scp-lab-srv/
echo $?
Output
scp: stat local "2222": No such file or directory
255

The right way to write it is scp -P 2222 a.txt srv:scp-lab-srv/. If that port is not open on the server, the message comes from ssh and the code is also 255:

Output of scp -P 2223 a.txt srv:scp-lab-srv/
ssh: connect to host srv port 2223: Connection refused
scp: Connection closed

The name printed is the one of your server (with HostName localhost, you will read localhost).

Understand scp in 2 minutes

scp opens an SSH connection to srv: everything that travels between your machine and the server is encrypted. The map follows the copy of projet to srv:dest/ and shows the effect of each option; its dest/ stands for your scp-lab-srv/.

Before using it, predict three cases: scp -p projet srv:dest/ (without -r), then scp -r projet srv:dest/, then scp -r -p projet srv:dest/. What does dest/ hold at the end, and which dates show on the server? Then check on the map: look at the option cards, at the content of srv:dest/ and at the "Dates on the server" screen.

Mental model

How scp copies a directory

open the SSH connectionread one item of the sourcewrite it on the server for each file to copy

$ scp projet srv:dest/

  1. scp opens an SSH connection to srv. If ssh srv fails, scp fails too.
  2. projet is a directory. Without -r, scp refuses it and copies nothing.
  3. Nothing is copied, the exit code is 1.

What the animation shows

  1. The source on the left, the destination on the right, like cp. The colon names the server.
  2. A directory needs `-r`, and the trailing slash changes nothing.
  3. Without `-p`, the copy takes the current date.
  4. A file already there is overwritten, without asking.

Golden rules

Check SSH first

If this command fails, scp will fail:

ssh srv true

Write the port with a capital P

Lower-case -p keeps the dates:

scp -P 2222 a.txt srv:dest/

Essential vocabulary

Copy · what leaves or comes back

a.txt srv:
Send a filescp a.txt srv:dest/; echo $? → code 0
srv:a.txt
Bring a file backscp srv:dest/a.txt recu.txt; cat recu.txt → recu.txt
projet/*
The content onlyscp -r projet/* srv:dest/; ssh srv ls dest → sub and t.txt
"mon f.txt"
A name with a spacecp a.txt 'mon f.txt'; scp 'mon f.txt' srv:dest/; ssh srv ls dest → arrived whole

Check · what happens on arrival

a.txt srv:
Overwrites without askingscp a.txt srv:dest/; ssh srv cat dest/a.txt → NOUVEAU
projet srv:
A directory without -rscp projet srv:dest/; echo $? → code 1
srv:absent/
A missing directoryscp a.txt srv:absent/x.txt; echo $? → code 1
-p 2222
The lower-case pscp -p 2222 a.txt srv:dest/ → looks for a file named 2222

Silent overwrite without confirmation

scp overwrites the destination without asking anything: there is no equivalent of cp -i. The server has scp-lab-srv/a.txt with ANCIEN, and your a.txt contains NOUVEAU.

Terminal window
ssh srv cat scp-lab-srv/a.txt
scp a.txt srv:scp-lab-srv/
ssh srv cat scp-lab-srv/a.txt
Output
ANCIEN
NOUVEAU

The old content is gone without a message. Before sending over an important file, bring it back first with scp srv:scp-lab-srv/a.txt a.txt.bak: that way you keep a local copy. The same risk exists the other way round: a download overwrites the local file of the same name.

Send, modify, bring back, compare Exercise 2 of 2 · ~10 min · terminal

A colleague wants to modify a file directly on the server, then fetch it back home without losing the original file, and check what changed.

Send with the dates

Send a.txt into scp-lab-srv/ under the name copie.txt, keeping its date, and read the year of copie.txt on the server.

Modify on the server

Add the line AJOUT at the end of copie.txt, directly from your terminal with ssh.

Bring back under another name

Fetch copie.txt locally under the name retour.txt, without touching a.txt.

Compare

Compare a.txt and retour.txt, then count the lines of retour.txt.

A hint 1 / 1

To add a line at the end of a file, the >> redirection does not overwrite it. diff compares two files, wc -l counts lines.

I have tried: show the solution
Terminal window
scp -p a.txt srv:scp-lab-srv/copie.txt
ssh srv 'cd scp-lab-srv && find copie.txt -printf "%TY %p\n"'
ssh srv 'echo AJOUT >> scp-lab-srv/copie.txt'
scp srv:scp-lab-srv/copie.txt retour.txt
diff a.txt retour.txt
wc -l < retour.txt
Outputs of find, diff and wc -l
2020 copie.txt
1a2
> AJOUT
2

The upload with -p keeps the 2020 date on copie.txt. The change on the server adds a line, and retour.txt contains NOUVEAU then AJOUT, while a.txt did not move: scp only overwrites the destination.

Spaces in remote paths

The local shell splits a name at spaces. Put the name between single or double quotes, whether the file is local or remote:

Terminal window
cp a.txt 'mon f.txt'
scp 'mon f.txt' srv:scp-lab-srv/
ssh srv 'ls "scp-lab-srv/mon f.txt"'
Output
scp-lab-srv/mon f.txt

The whole name was passed on. To bring this file back, the remote path with a space is protected the same way:

Terminal window
scp 'srv:scp-lab-srv/mon f.txt' copie-espace.txt
cat copie-espace.txt
Output
NOUVEAU

Avoid -O

Without an option, scp transfers through the SFTP subsystem of OpenSSH (since version 9.0). With -O, it falls back to the old protocol: the shell of the server interprets the name of the remote file, so a space, a ; or a $(…) changes meaning there. Only use -O against an old server without SFTP, and never with a name you do not control.

Use a specific SSH key

For a script, use a dedicated key: it avoids a password in clear text and can be revoked without touching other accesses. -i chooses the key, as with ssh. Replace the path with the one of your key.

Terminal window
scp -i ~/.ssh/id_ed25519 a.txt srv:scp-lab-srv/cle.txt

The command prints nothing and returns code 0. To stop writing -i, put IdentityFile ~/.ssh/id_ed25519 in the Host srv block of ~/.ssh/config.

Compression and bandwidth

-C turns compression on: useful for text over a slow link, useless on an archive or an image that is already compressed. -l caps the rate, in Kbit/s and not in KB/s.

Unit of -l: Kbit/s, not KB/s

gros.bin weighs 1 MB, that is 8,000 Kbit. At 2,000 Kbit/s, the copy therefore takes about 4 seconds; at 8,000 Kbit/s, about 1 second. -q removes the progress bar, time measures the duration.

Terminal window
time scp -q gros.bin srv:scp-lab-srv/gros1.bin
time scp -q -l 2000 gros.bin srv:scp-lab-srv/gros2.bin
time scp -q -l 8000 gros.bin srv:scp-lab-srv/gros3.bin
Real durations of the three copies
real 0m0,108s
real 0m3,997s
real 0m0,874s

The duration depends on the size and on the limit. Since 800 Kbit/s is about 100 KB/s, write -l 800, not -l 100, if you aim at 100 KB/s.

When to switch to rsync

scp suits a single copy. As soon as a copy repeats, or the file is big, rsync does better: it only copies what changed, it can simulate with -n and exclude files. After an interruption, scp starts again from zero, whereas rsync -P gros.bin srv:scp-lab-srv/ keeps the fragment already received and resumes from there. In return, rsync must be installed on both sides, whereas scp comes with the SSH client.

Which command should you choose?

The choice depends on how often the copy happens and on what you want to do on the server.

NeedCommand
Single copy of a file or a folder to a serverscp
Repeated backup or deployment, resume after an interruptionrsync
Browse a server, several files in one sessionsftp
A compressed archive to send, with precise permissionstar then SSH
Local copycp

Troubleshooting

Start with the method, then look for your message in the table: each line gives the cause and the fix.

Diagnostic method

Run ssh srv true first: if the connection fails, look on the SSH side, not on the scp side. Otherwise run again with scp -v: the useful line is almost always the last one before the failure. The first line of the error names the path and the cause; the paths are those of the server, except local, which designates your machine.

Common errors

MessageCauseSolution
scp: dest open "ro/a.txt": Permission deniedNo write permission on the remote folder or fileAim at an accessible folder, check with ssh srv ls -ld ro
scp: dest open "/etc/a.txt": Permission deniedWriting into a system folder with an ordinary accountCopy into your home folder, then ssh -t srv sudo mv; never a root login to write into /etc
scp: dest open "absent/x.txt": No such file or directoryDestination folder missing (with a destination absent/, the message is dest open "absent/": Failure)Create the folder with ssh srv mkdir absent
scp: open local "illisible.txt": Permission deniedLocal file not readableRead the local permissions with ls -l
ssh: connect to host … port 2223: Connection refused, code 255Wrong port, sshd stopped or firewallCheck the port with -P
subsystem request failed on channel 0, then scp: Connection closed (code 255)The SSH server has no SFTP subsystemOn the server, Subsystem sftp internal-sftp in /etc/ssh/sshd_config, then restart the service
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!The server key no longer matches known_hostsCheck with the administrator that the server was reinstalled, then ssh-keygen -R srv (the SSH client)
zsh: no matches found: srv:scp-lab-srv/*.logThe local shell expands the wildcard before scpQuote it: scp 'srv:scp-lab-srv/*.log' .
No space left on deviceDisk full on the destinationFree some space, check with ssh srv df -h
A local file was replaced by a downloadscp overwrites without askingBack up first: cp a.txt a.txt.bak

Clean up the practice folder

Delete the local practice folder and the folder on the server, the only two the page created. With localhost, scp-lab-srv is in your home folder: re-read the name before running rm -r.

Terminal window
cd ~
rm -r ~/scp-lab
ssh srv rm -r scp-lab-srv

Key points

  • The source is on the left, the destination on the right; the srv: prefix designates the server, and its position gives the direction of the copy. A successful copy is silent: check on the server side.
  • A folder needs -r. A missing destination takes the name of the source, an existing destination receives it as a subfolder. projet/* does not send hidden files such as .env.
  • -p (lowercase) keeps dates and permissions, -P (uppercase) chooses the port: a -p 2222 is read as a file name.
  • scp overwrites without asking; only the permissions of the remote file prevent it (Permission denied).
  • -l is expressed in Kbit/s, not in KB/s. Without an option, scp uses SFTP; -O gives the remote name to the shell of the server, to be avoided.
  • To repeat or resume a copy, use rsync.

Knowledge check

Check that the essentials of this guide stuck. The questions cover only what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

6 questions
6 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer