How to Copy Files Over SSH in Linux
Move files between machines with scp and rsync, over the same encrypted connection.
Both tools ride SSH, so they inherit your keys, your ~/.ssh/config aliases and the encryption. The difference is what they send: scp copies everything, every time; rsync compares both ends and transfers only what changed. That makes the choice simple — one file once, scp; anything repeated or large, rsync.
Step by step
1 Send a file
$ scp report.pdf user@server:/tmp/ Source first, destination second — same order as cp. The trailing slash makes it explicit that the target is a directory.
2 Fetch a file
$ scp user@server:/var/log/app.log . Reverse the arguments to pull instead of push. The dot means "into the current directory".
3 Copy a whole directory
$ rsync -avP site/ user@server:/var/www/site/ Archive mode preserves permissions and timestamps, -P shows progress and allows resuming. Run it again tomorrow and only the changed files travel.
4 Mirror, excluding junk
$ rsync -avn --delete --exclude=".git" --exclude="node_modules" src/ user@server:/app/ Note the -n: this is a dry run. Read what it would do, and only then run it again without the -n. Make this a habit with --delete.
5 Non-default port
$ scp -P 2222 file user@server:/tmp/
rsync -avP -e "ssh -p 2222" dir/ user@server:/dst/ scp uses capital -P for the port (lowercase -p means "preserve"). rsync passes the whole ssh command with -e.
Tips worth knowing
- The trailing slash in rsync is not cosmetic: src/ means "the contents of src", src means "the directory itself". This causes more rsync surprises than everything else combined.
- Define servers once in ~/.ssh/config and both tools become `scp file web1:/tmp/` — no user, no IP, no port to remember.
- scp cannot resume an interrupted transfer; rsync -P can. On a bad connection that decides it for you.
- Cannot write to the destination? Copy to /tmp and move it with sudo on the server — neither tool can elevate privileges on the far side.
Frequently asked questions
scp or rsync?
scp for a single file, once. rsync for directories, repeated syncs, resumable transfers or anything where only part of the data changes.
Why is the port flag -P in scp but -p in ssh?
scp inherited -p from cp, where it means "preserve timestamps", so the port needed a different letter. Everyone trips on this once.
Can I copy between two remote servers?
Yes: scp user@a:/file user@b:/dir/ works, though the data relays through your machine by default. For large transfers, run rsync on one of the servers directly.
How do I copy without typing a password every time?
Set up key authentication: ssh-keygen -t ed25519 then ssh-copy-id user@server. Both scp and rsync then use the key automatically.
If it goes wrong
SSH rejected your login because no key you offered was accepted by the server.
Connection refusedYou reached the machine, but nothing was listening on that port — or a firewall said no.
The commands behind it
The quickest way to move a file to or from a remote machine.
rsyncSync directories locally or over SSH, transferring only what changed.
sshLog into remote machines, run remote commands, use keys instead of passwords, and tunnel ports.
Concepts involved: SSH key
Choosing between tools
More how-to guides
See how much space is left, and find out what is using it.
Extract a .tar.gz archiveUnpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.
Free a port that is already in useFind which process holds a port and stop it cleanly.
Find and replace text in filesReplace text in one file or across a whole project, safely.