Knowledge
How to copy files over SSH with scp
#CommandLine
Copy files and directories between your machine and a remote server over SSH using scp, including custom ports, key files and host-to-host transfers.
Published by Mark van Eijk on June 23, 2026 · 2 minute read
- Copying files over SSH
- Local to remote
- Remote to local
- Copying directories with -r
- Using a custom port with -P
- Using a specific key with -i
- Copying between two remote hosts
- A few tips
Copying files over SSH
Whenever I need to move a file to or from a server, scp is the tool I reach for first. It rides on top of SSH, so if you can already connect to the server over SSH, you can copy files to it without any extra setup. The general syntax is scp <source> <destination>, where a remote location looks like user@host:/path.
Local to remote
To upload a file from your machine to a server, put the remote location on the right:
scp ./backup.sql deploy@example.com:/var/www/backups/
This copies backup.sql into the /var/www/backups/ directory on the server. If you want to rename it on the way, just include a filename in the destination path.
Remote to local
Flip the arguments to download instead. Here the remote path is the source, and . is the current local directory:
scp deploy@example.com:/var/log/nginx/error.log .
Copying directories with -r
By default scp only handles single files. To copy a whole directory and everything inside it, add -r (recursive):
scp -r ./public deploy@example.com:/var/www/app/
Using a custom port with -P
If the server's SSH daemon listens on a non-standard port, pass it with -P (a capital P — lowercase -p preserves timestamps instead):
scp -P 2222 ./config.yml deploy@example.com:/etc/app/
Using a specific key with -i
When the server expects a particular private key, point at it with -i. This is handy when you juggle several keys:
scp -i ~/.ssh/deploy_key ./release.tar.gz deploy@example.com:/tmp/
If you'd rather not type this every time, you can configure the key per host in ~/.ssh/config and scp will pick it up automatically.
Copying between two remote hosts
You don't even need the file to pass through your machine. Give two remote locations and scp copies directly between them:
scp deploy@host1.example.com:/data/dump.sql deploy@host2.example.com:/data/
Add -3 if the two hosts can't reach each other directly and you want the transfer routed through your local machine instead.
A few tips
You can combine flags, for example scp -rP 2222 to recursively copy over a custom port. And if you're moving large directories around regularly, rsync is often a better fit since it only transfers what changed — but for a quick one-off copy, scp is hard to beat.
Subscribe to our newsletter
Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!
Related articles
Argument list too long (Bash: /bin/rm)
Copy files and directories between your machine and a remote server over SSH using scp, including custom ports, key files and host-to-host transfers.
How to install Composer packages locally
Copy files and directories between your machine and a remote server over SSH using scp, including custom ports, key files and host-to-host transfers.