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

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

Permission denied (publickey)

SSH rejected your login because no key you offered was accepted by the server.

Connection refused

You reached the machine, but nothing was listening on that port — or a firewall said no.

The commands behind it

scp

The quickest way to move a file to or from a remote machine.

rsync

Sync directories locally or over SSH, transferring only what changed.

ssh

Log into remote machines, run remote commands, use keys instead of passwords, and tunnel ports.

Concepts involved: SSH key

Choosing between tools

rsync vs scp

Both copy files over SSH. rsync sends only what changed; scp sends everything, every time.

More how-to guides

Check disk space in Linux

See how much space is left, and find out what is using it.

Extract a .tar.gz archive

Unpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.

Free a port that is already in use

Find which process holds a port and stop it cleanly.

Find and replace text in files

Replace text in one file or across a whole project, safely.