# scp — Copy Files Securely over SSH

> Practical guide to scp and sftp — transfer files over SSH between local and remote hosts, with recursive copies, key authentication and sftp sessions.

Source: https://www.jpkc.com/db/en/cheatsheets/networking/scp/

<!-- PROSE:intro -->
scp copies files between a local machine and a remote host over an encrypted SSH connection – no extra software, no separate protocol. Source and destination follow familiar cp syntax; SSH handles the encryption transparently. For large or recurring syncs, consider rsync for its incremental transfers; sftp provides an interactive FTP-like session when you need to browse the remote filesystem.
<!-- PROSE:intro:end -->

## scp — Copy to Remote

`scp <file> <user>@<host>:<path>` — Copy a local file to a remote host.

```bash
scp config.yaml admin@server:/etc/app/
```

`scp -r <dir> <user>@<host>:<path>` — Copy a directory recursively to remote.

```bash
scp -r ./dist/ deploy@server:/var/www/html/
```

`scp -P <port> <file> <user>@<host>:<path>` — Copy using a custom SSH port.

```bash
scp -P 2222 backup.tar.gz admin@server:/backups/
```

`scp -i <key> <file> <user>@<host>:<path>` — Copy using a specific SSH key.

```bash
scp -i ~/.ssh/deploy_key app.tar.gz deploy@server:/opt/
```

## scp — Copy from Remote

`scp <user>@<host>:<path> <local>` — Copy a file from remote to local.

```bash
scp admin@server:/var/log/app.log ./logs/
```

`scp -r <user>@<host>:<path> <local>` — Copy a directory from remote to local.

```bash
scp -r admin@server:/etc/nginx/ ./nginx-backup/
```

`scp <user>@<host1>:<path> <user>@<host2>:<path>` — Copy directly between two remote hosts.

```bash
scp admin@server1:/data/dump.sql admin@server2:/data/
```

## scp — Options

`scp -C <file> <user>@<host>:<path>` — Enable compression during transfer.

```bash
scp -C largefile.sql admin@server:/tmp/
```

`scp -l <kbit/s> <file> <user>@<host>:<path>` — Limit bandwidth (in Kbit/s).

```bash
scp -l 5000 backup.tar.gz admin@server:/backups/
```

`scp -p <file> <user>@<host>:<path>` — Preserve file timestamps and permissions.

```bash
scp -p important.conf admin@server:/etc/app/
```

`scp -q <file> <user>@<host>:<path>` — Quiet mode: suppress progress meter.

```bash
scp -q script.sh admin@server:/usr/local/bin/
```

## sftp — Interactive Session

`sftp <user>@<host>` — Start an interactive SFTP session.

```bash
sftp admin@server
```

`get <remote-file>` — Download a file (inside sftp session).

```bash
sftp> get /var/log/app.log
```

`put <local-file>` — Upload a file (inside sftp session).

```bash
sftp> put config.yaml /etc/app/
```

`mget <pattern>` — Download multiple files matching a pattern.

```bash
sftp> mget *.log
```

`mput <pattern>` — Upload multiple files matching a pattern.

```bash
sftp> mput *.conf
```

`ls` — List remote directory contents.

```bash
sftp> ls -la
```

`lls` — List local directory contents.

```bash
sftp> lls
```

## sftp — Batch Mode

`sftp -b <batchfile> <user>@<host>` — Run SFTP commands from a batch file.

```bash
sftp -b commands.txt admin@server
```

`sftp -P <port> <user>@<host>` — Connect on a custom SSH port.

```bash
sftp -P 2222 admin@server
```

`sftp -i <key> <user>@<host>` — Connect using a specific SSH key.

```bash
sftp -i ~/.ssh/deploy_key deploy@server
```

<!-- PROSE:outro -->
## Conclusion

scp handles quick one-off transfers reliably as long as SSH is reachable – no extra daemons, no configuration overhead. For recurring or bandwidth-sensitive syncs, `rsync` is the better choice; for interactive browsing, fall back to `sftp`. Pair it with an SSH key and you eliminate password prompts entirely.

## Further Reading

- [Secure copy protocol – Wikipedia](https://en.wikipedia.org/wiki/Secure_copy_protocol) – background and history
- [scp(1) – OpenSSH manual page](https://man.openbsd.org/scp) – every option at a glance
<!-- PROSE:outro:end -->

## Related Commands

- [ssh](https://www.jpkc.com/db/en/cheatsheets/networking/ssh/) – remote shell over SSH – the transport scp is built on
- [ssh-keygen](https://www.jpkc.com/db/en/cheatsheets/networking/ssh-keygen/) – generate an SSH key pair for passwordless scp transfers
- [mosh](https://www.jpkc.com/db/en/cheatsheets/networking/mosh/) – resilient SSH alternative for unreliable connections

