How to copy files over SSH with scp - Rocketeers app

  [ Rocketeers ](/)   

[Login](https://rocketeersapp.com/login) 

 On this page

 Knowledge
---------

How to copy files over SSH with scp
===================================

### [\#CommandLine](https://rocketeersapp.com/knowledge/command-line)

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](https://rocketeersapp.com/author/mark-van-eijk) on June 23, 2026 · 2 minute read

1. [Copying files over SSH](#content-copying-files-over-ssh)
2. [Local to remote](#content-local-to-remote)
3. [Remote to local](#content-remote-to-local)
4. [Copying directories with -r](#content-copying-directories-with--r)
5. [Using a custom port with -P](#content-using-a-custom-port-with--p)
6. [Using a specific key with -i](#content-using-a-specific-key-with--i)
7. [Copying between two remote hosts](#content-copying-between-two-remote-hosts)
8. [A few tips](#content-a-few-tips)

[\#](#content-copying-files-over-ssh "Permalink")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](/connect-to-server-ssh-command), you can copy files to it without any extra setup. The general syntax is `scp  `, where a remote location looks like `user@host:/path`.

[\#](#content-local-to-remote "Permalink")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.

[\#](#content-remote-to-local "Permalink")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 .

```

[\#](#content-copying-directories-with--r "Permalink")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/

```

[\#](#content-using-a-custom-port-with--p "Permalink")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/

```

[\#](#content-using-a-specific-key-with--i "Permalink")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.

[\#](#content-copying-between-two-remote-hosts "Permalink")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.

[\#](#content-a-few-tips "Permalink")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!

  Fill in your email address to receive updates  Subscribe 

#### More in [\#CommandLine](https://rocketeersapp.com/knowledge/command-line)

- [Argument list too long (Bash: /bin/rm)](https://rocketeersapp.com/knowledge/argument-list-too-long)
- [How to install Composer packages locally](https://rocketeersapp.com/knowledge/install-composer-packages-locally)
- [How to send GET and POST requests with curl](https://rocketeersapp.com/knowledge/curl-post-get-api-requests)
- [Essential Linux command line basics for developers](https://rocketeersapp.com/knowledge/linux-command-line-basics)
- [How to search file contents with grep](https://rocketeersapp.com/knowledge/search-files-grep-command)
- [The complete guide to the curl command](https://rocketeersapp.com/knowledge/curl-command-complete-guide)

 [View all 21 articles →](https://rocketeersapp.com/knowledge/command-line)
