Copy files to a server with scp and rsync
scp and rsync copy files between your computer and a server over SSH, and rsync only sends what changed.
- Updated Nov 27, 2026
- Markdown
Copy files and folders to a server with scp and rsync, and preview a sync safely.
- A server you can log in to with SSH
- A file or folder to copy
To copy files to a server, use scp file user@host:/path/ for one file or rsync -avz folder/ user@host:/path/ for a folder. You build a site on your laptop and need it on the server. Or a database dump sits on the server and you want a copy. Both tools send files through the same encrypted SSH connection you already use to log in, so there is no extra service to set up and no extra port to open.
How it works#
scp is a plain copy. You name a source and a destination, and it sends each file whole. A remote location is written user@host:path. The part after the colon is the path on the server, and a relative path starts in that user’s home directory.
rsync is a synchroniser. It compares the source and the destination, then sends only the parts that differ. The first run copies everything. Later runs are quick, because unchanged files are skipped. It also keeps file times and permissions when you ask it to, and can resume a broken transfer.
Both start ssh (see What is SSH) underneath, so everything in your SSH config (user, port, key) applies. Both need a login the server accepts. rsync also needs the rsync program on both ends, since the two sides talk to each other.
| Task | Use |
|---|---|
| One file, once | scp |
| A folder, or repeating it | rsync |
| A large file on a flaky link | rsync --partial |
| Make the server match a folder exactly | rsync --delete (preview first) |
Replace deploy, 203.0.113.10 and the paths with your own in the commands below.
Step 1: Upload a file with scp#
scp ./backup.tar.gz deploy@203.0.113.10:/home/deploy/What you should see: a progress line with the file name, size, speed and time, ending at 100%. Download a file by swapping the order:
scp deploy@203.0.113.10:/var/log/app.log ./Add -r to copy a folder, and -P 2222 (capital P) for a custom SSH port. The capital is different from ssh -p, which is lower case.
Step 2: Sync a folder with rsync#
rsync -avz ./site/ deploy@203.0.113.10:/home/deploy/site/Flags: -a is archive mode (recurse and keep permissions and times), -v prints files, -z compresses in transit. What you should see: a list of file names, then a summary line with bytes sent and received. Run it again and only changed files are sent, so the list is short or empty.
Step 3: Preview before you sync#
Warning: the
--deleteflag removes files on the destination that are not in the source. Always preview with--dry-runfirst.
rsync -avz --delete --dry-run ./site/ deploy@203.0.113.10:/home/deploy/site/What you should see: the same kind of listing, with deleting in front of any file that would be removed, and the summary says (DRY RUN). Nothing is changed. Remove --dry-run only when the list looks right.
Step 4: Resume a big transfer#
rsync -avz --partial --progress ./big.iso deploy@203.0.113.10:/home/deploy/--partial keeps the half-sent file instead of deleting it, and --progress shows a live percentage. If the connection drops, run the same command again to continue.
The trailing slash#
./site/ copies the contents of site into the destination. ./site copies the folder itself, creating site/site. The slash on the destination side does not matter, but on the source it does. Check this twice, especially with --delete.
Check it worked#
ssh deploy@203.0.113.10 'ls -l /home/deploy/site'You should see your files with sizes that match the originals. For a stronger check on one file, compare checksums on both sides:
sha256sum ./backup.tar.gz
ssh deploy@203.0.113.10 'sha256sum /home/deploy/backup.tar.gz'What you should see: two lines with the same long hex string.
Edge cases and gotchas#
- Trailing slash decides where files land. The most common mistake is
site/siteon the server. A dry run shows it before it happens. - IPv6 addresses need brackets in scp. Write
deploy@[2001:db8::10]:/tmp/. Without them, the colons confuse the host and path parsing. - Spaces in remote paths need careful quoting. Quote the whole remote argument, and for scp you may need an extra level of quotes. Avoiding spaces in server paths removes the problem.
- Ownership is not copied unless you are root. rsync
-atries to keep owner and group, but a normal user cannot give files to someone else. Files end up owned by the login user. - Hidden files are included for folders. Copying a folder takes dotfiles such as
.envwith it. Check what is inside before you upload, and use--exclude '.env'or--exclude '.git'to leave things out. - scp behaviour changed in recent OpenSSH. Newer OpenSSH versions run scp over the SFTP protocol, which can handle wildcards and special characters differently from older releases. If a pattern copies on one machine and not another, that is a likely reason; rsync avoids it.
- Slow links need compression only for text.
-zhelps with logs and source code, but already-compressed files (zip, jpg, mp4) gain nothing and cost CPU.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
Permission denied on the destination | Your user cannot write there | Copy into your home, then move with sudo on the server |
Permission denied (publickey) | Wrong user or key | Check the user name and your key, see Log in with SSH keys |
rsync: command not found | rsync missing on the server | sudo apt install rsync on the server |
Connection refused | SSH is not reachable | Check the IP and port, and the firewall |
scp: /path: No such file or directory | Wrong remote path, or typo | Check it with ssh deploy@203.0.113.10 'ls /path' |
| Everything is copied again each run | Source path has no trailing slash, or times differ | Use the trailing slash and -a so times are kept |
rsync error: unexplained error (code 255) | The SSH connection itself failed | Run the ssh command alone and fix that first |
rsync: [sender] change_dir ... failed: No such file or directory | The source path is wrong | Check it with ls on the sending side |
Host key verification failed. | The saved server fingerprint changed | Confirm why, then ssh-keygen -R <address> |
Real-world scenario#
You build a static site into ./site on your laptop and need it on a server at 203.0.113.10. The first deploy is rsync -avz ./site/ deploy@203.0.113.10:/var/www/site/, which sends every file.
A week later you change one page and run the same command. rsync sends only that page and prints a short summary. Then you delete an old image locally and want the server to match. You run the command with --delete --dry-run first and see deleting images/old.png in the list, so you know the plan is right and run it for real.
Before you did this you once forgot the trailing slash and ended up with /var/www/site/site. The dry run now catches that.
Quick reference#
| Task | Command |
|---|---|
| Upload a file | scp file user@host:/path/ |
| Download a file | scp user@host:/path/file ./ |
| Copy a folder | scp -r dir user@host:/path/ |
| Sync a folder | rsync -avz ./dir/ user@host:/path/dir/ |
| Preview deletes | rsync -avz --delete --dry-run SRC DEST |
| Resume | rsync -avz --partial --progress SRC DEST |
| Custom port | scp -P 2222 or rsync -e "ssh -p 2222" |
Security and safety notes#
--deleteremoves files on the destination. Always run--dry-runfirst.- Do not upload
.envfiles, private keys or database dumps to a place others can read.
Related lessons#
- Log in with SSH keys: copy files without typing a password
- SSH config file shortcuts: shorter host names for scp and rsync
- Linux file permissions explained: why a copy can end up unreadable
- Back up a Postgres database in Docker: a dump you might copy off the server
Next#
Stop retyping the options with SSH config file shortcuts.
Check what you learned
Frequently asked questions
Should I use scp or rsync?
Use scp for a quick single file. Use rsync for folders, large transfers and repeated syncs, because it skips unchanged files and can resume.
Do these need anything installed on the server?
Both use your existing SSH login. rsync must also be installed on both ends, and on Ubuntu you can add it with sudo apt install rsync.
How do I copy to a server that uses a different SSH port?
With scp use a capital P, as in scp -P 2222. With rsync pass the whole ssh command, as in rsync -e "ssh -p 2222". An alias in your SSH config avoids both.
Can I copy a file straight into a folder only root can write?
Not as a normal user. Copy to your home directory first, then move it with sudo on the server. Or give your user ownership of the target folder if that is appropriate.
Sources
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Manage Secrets Self-Hosted: Env Vars and Master Key Rotation Set env vars and encrypted secrets on a self-hosted server, apply changes safely, and rotate the master key with no downtime without locking yourself out.