- The pattern is always
scp [options] source destination, where a remote path isuser@host:/path. - Copying a folder needs
-r. Without it, scp skips the folder. -P(uppercase) sets the port. Lowercase-pkeeps timestamps, which is a different flag entirely.- For anything over a few hundred megabytes, or anything you might run twice, use rsync: it resumes and skips unchanged files.
- Put quotes around remote paths with spaces or wildcards, because the remote server's shell reads them.
What is the basic scp command syntax?
scp copies files between machines over SSH (the encrypted connection you use to log in to a server), with the same keys and settings. Write the source first and the destination second, like cp. A remote location is user@host:/path, and the side with the colon is the remote one.
# Local file to remote server
scp report.pdf deploy@203.0.113.10:/home/deploy/
# Remote file to local machine
scp deploy@203.0.113.10:/var/log/nginx/error.log ./
# Whole directory, both ways (note -r)
scp -r ./site-files deploy@203.0.113.10:/var/www/
scp -r deploy@203.0.113.10:/var/www/example.com ./backup/
# Between two remote servers, routed through your machine
scp -r deploy@server-a:/var/www/site deploy@server-b:/var/www/Without -r, folders are skipped
Older versions of scp print "not a regular file" and carry on; newer ones fail. Either way, the folder does not arrive. If a copy seemed to work but nothing is there, check for a missing -r first.
Which scp options actually matter?
| Flag | Does | Use it when |
|---|---|---|
| -r | Copy folders and everything in them | Whenever the source is a folder |
| -P 2222 | Connect to a non-default SSH port | Your server does not use port 22 |
| -p | Keep modification times and permissions | The timestamps matter, such as a backup |
| -C | Compress in transit | Text, code or SQL over a slow link |
| -i ~/.ssh/key | Use a specific private key | You have more than one key |
| -l 8000 | Limit to 8000 Kbit/s | Copying on a live server during working hours |
| -q | Quiet: no progress meter | Running inside a script or cron job |
| -v | Verbose SSH debugging | The connection fails and you need to know why |
Uppercase -P is the port. Lowercase -p keeps timestamps. Mixing them up is the most common scp mistake.
# Non-standard port, specific key, preserve timestamps
scp -P 2222 -i ~/.ssh/deploy_key -p backup.tar.gz deploy@203.0.113.10:/var/backups/
# Compress a SQL dump in transit (large win on text)
scp -C database.sql deploy@203.0.113.10:/tmp/
# Throttle to roughly 1 MB/s so a live site keeps its bandwidth
scp -l 8000 -r ./uploads deploy@203.0.113.10:/var/www/example.com/wp-content/
# Diagnose a failing connection
scp -v file.txt deploy@203.0.113.10:/tmp/Compression helps text and nothing else. SQL dumps, logs and source code compress well. JPEGs, PNGs, MP4s and archives that are already gzipped do not, so -C only adds work on both ends.
How do I copy multiple files or use wildcards?
Your own shell expands local wildcards before scp runs. The remote server's shell expands remote wildcards, so they must get past your local shell untouched. That is why remote patterns need quotes.
# Several named local files to one destination
scp report.pdf invoice.pdf notes.txt deploy@203.0.113.10:/home/deploy/docs/
# Local wildcard: expanded by YOUR shell, no quotes needed
scp ./logs/*.log deploy@203.0.113.10:/tmp/logs/
# Remote wildcard: MUST be quoted, or your local shell eats it
scp 'deploy@203.0.113.10:/var/log/nginx/*.log' ./logs/
# Brace expansion works remotely too, inside quotes
scp 'deploy@203.0.113.10:/etc/nginx/{nginx.conf,mime.types}' ./config/
# Paths with spaces need escaping twice: once locally, once remotely
scp 'deploy@203.0.113.10:/home/deploy/My\ Documents/file.pdf' ./There is no --exclude in scp
scp cannot skip files. To copy a site without node_modules, cache folders or logs, use rsync, or tar the folder with --exclude first and copy the archive. Creating tar archives covers the exclude syntax.
When should I use rsync instead of scp?
Use scp for a one-off copy of a few files, and rsync for everything else. rsync sends only what has changed, picks up an interrupted transfer where it stopped, and can leave paths out. That makes it the right tool for site moves and repeat syncs. It uses the same SSH connection, so nothing else in your setup changes.
| scp | rsync | |
|---|---|---|
| Resumes after a drop | No, starts from zero | Yes, with --partial |
| Skips unchanged files | No, copies everything | Yes, that is the point |
| Exclude patterns | No | Yes, --exclude |
| Progress detail | One overall bar | Per file plus a total |
| Delete removed files | No | Yes, with --delete |
| Installed by default | Yes, everywhere | Usually, but not always |
The first two rows matter most on a large transfer that fails at 90 percent.
# Standard: archive mode, compress, human-readable progress
rsync -avzh --progress ./site/ deploy@203.0.113.10:/var/www/example.com/
# Resume a large transfer that was interrupted
rsync -avzh --partial --progress big.tar.gz deploy@203.0.113.10:/var/backups/
# Skip what should never be copied
rsync -avzh --progress \
--exclude='node_modules' \
--exclude='wp-content/cache' \
--exclude='*.log' \
./site/ deploy@203.0.113.10:/var/www/example.com/
# Non-standard SSH port
rsync -avzh -e 'ssh -p 2222' ./site/ deploy@203.0.113.10:/var/www/
# See exactly what WOULD happen, change nothing
rsync -avzh --dry-run --delete ./site/ deploy@203.0.113.10:/var/www/example.com/./site/ copies the contents, ./site copies the folder itself into the destination.Always dry-run --delete first
--delete removes anything at the destination that is not at the source. With a mistyped source path or a missing trailing slash, it will empty the target folder. Run it with --dry-run first, every time, and read the output.
How do I fix common scp errors?
| Error | Cause | Fix |
|---|---|---|
| Permission denied (publickey) | SSH key not accepted | Check with ssh -v user@host; scp logs in the same way |
| No such file or directory | The destination folder does not exist | scp does not create paths: mkdir -p on the remote first |
| not a regular file | Source is a folder and -r is missing | Add -r |
| Permission denied (after connecting) | The remote user cannot write there | Copy to /tmp, then move it with sudo |
| Connection refused | Wrong port, or the SSH service is not running | Add -P with the correct port |
| scp: command not found on remote | Newer OpenSSH uses the SFTP protocol by default | Add -O to force the older transfer mode |
The last row catches people out: since OpenSSH 9, scp uses SFTP underneath by default, which fails against some older or locked-down servers.
# This fails: deploy cannot write directly into /var/www
scp site.tar.gz deploy@203.0.113.10:/var/www/
# Do it in two steps instead
scp site.tar.gz deploy@203.0.113.10:/tmp/
ssh deploy@203.0.113.10 'sudo mv /tmp/site.tar.gz /var/www/ && sudo chown deploy:www-data /var/www/site.tar.gz'Ownership does not survive the copy
Files arrive owned by the user you connected as, not by whoever owned them at the source. After copying site files, you usually need to reset ownership and permissions. Linux file permissions for web servers has the exact commands.
Moving a whole WordPress site takes more than copying files: the database, the addresses stored inside it and the DNS switch matter as much. We move WordPress sites for you: files, database, email and DNS, and you check the result before anything switches.
Frequently asked questions
How do I copy a folder with scp?
Add -r: scp -r ./localfolder user@host:/remote/path/. Without it, scp skips the folder and the copy looks fine while nothing arrives. The destination path must already exist, because scp does not create folders.
What is the difference between -p and -P in scp?
Uppercase -P sets the SSH port, for example -P 2222. Lowercase -p keeps modification times and permissions. They are unrelated, and mixing them up gives you a connection error instead of the transfer you wanted.
Is scp faster than rsync?
For a first copy of one file they are about the same, since both use SSH. rsync wins everywhere else: it skips unchanged files on a repeat run, resumes an interrupted transfer instead of starting again, and can leave paths out. For a large or repeated copy, rsync is faster in practice.
How do I scp a file to a non-standard SSH port?
Use uppercase -P: scp -P 2222 file.txt user@host:/path/. rsync uses a different form for the same thing: rsync -e 'ssh -p 2222'. The difference catches people out often.
Why does scp say permission denied when I can SSH in fine?
Usually the remote user cannot write to the destination folder; the login itself is fine. Copy to /tmp first, then move the file with sudo over SSH. Do not turn on root login to work around it.
Is scp deprecated?
The old scp protocol is considered outdated, and OpenSSH 9 switched scp to use SFTP underneath by default. The scp command itself still ships everywhere and works. For scripted or large transfers, rsync or sftp are better long-term choices.
About G7Cloud Engineering
Articles written by the engineers who build and run G7Cloud: UK managed hosting and the AI Website Builder. We write about what we operate every day: containers, backups, databases, and the small-business websites that run on them.
More about G7Cloud →