Linux7 min readPublished: 15 Apr 2024Updated: 9 Nov 2026

SCP Command Examples: Copy Files Over SSH

Practical scp examples for copying files and folders between Linux servers over SSH, with the flags that matter and when rsync is the better tool for the job.

G7Cloud Engineering
Platform team
Share:
Abstract illustration representing scp command examples: copy files over ssh.
  • The pattern is always scp [options] source destination, where a remote path is user@host:/path.
  • Copying a folder needs -r. Without it, scp skips the folder.
  • -P (uppercase) sets the port. Lowercase -p keeps 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.

The four directions
# 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/
The last one is handy but slow: the data travels to your machine and out again. Add -3 to make that explicit, or run the command on one of the servers instead.

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?

FlagDoesUse it when
-rCopy folders and everything in themWhenever the source is a folder
-P 2222Connect to a non-default SSH portYour server does not use port 22
-pKeep modification times and permissionsThe timestamps matter, such as a backup
-CCompress in transitText, code or SQL over a slow link
-i ~/.ssh/keyUse a specific private keyYou have more than one key
-l 8000Limit to 8000 Kbit/sCopying on a live server during working hours
-qQuiet: no progress meterRunning inside a script or cron job
-vVerbose SSH debuggingThe 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.

Options in practice
# 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/
-l is in kilobits per second, so 8000 is about 1 MB/s. Use it on a live server in the middle of the day.
Pro Tip

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.

Multiple files, both directions
# 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' ./
The double escaping on the last line looks odd but is correct: once your quotes are stripped, the remote shell still needs its own backslash.

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.

scprsync
Resumes after a dropNo, starts from zeroYes, with --partial
Skips unchanged filesNo, copies everythingYes, that is the point
Exclude patternsNoYes, --exclude
Progress detailOne overall barPer file plus a total
Delete removed filesNoYes, with --delete
Installed by defaultYes, everywhereUsually, but not always

The first two rows matter most on a large transfer that fails at 90 percent.

The rsync commands worth memorising
# 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/
The trailing slash on the source matters: ./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?

ErrorCauseFix
Permission denied (publickey)SSH key not acceptedCheck with ssh -v user@host; scp logs in the same way
No such file or directoryThe destination folder does not existscp does not create paths: mkdir -p on the remote first
not a regular fileSource is a folder and -r is missingAdd -r
Permission denied (after connecting)The remote user cannot write thereCopy to /tmp, then move it with sudo
Connection refusedWrong port, or the SSH service is not runningAdd -P with the correct port
scp: command not found on remoteNewer OpenSSH uses the SFTP protocol by defaultAdd -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.

Copying to a directory you do not own
# 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'
This is the right pattern. Turning on root SSH login so scp can write anywhere swaps a small inconvenience for a large security problem.

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.

Put this into practice on G7Cloud

Every site runs in its own dedicated container behind ScaleShield, with daily backups that are restore-tested every night. Start on the free plan, no card needed.

About G7Cloud Engineering

Platform team

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 →