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.

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.

scp — Copy to Remote

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

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

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

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

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

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

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

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.

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

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

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

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

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

scp — Options

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

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

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

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

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

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

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

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

sftp — Interactive Session

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

sftp admin@server

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

sftp> get /var/log/app.log

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

sftp> put config.yaml /etc/app/

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

sftp> mget *.log

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

sftp> mput *.conf

ls — List remote directory contents.

sftp> ls -la

lls — List local directory contents.

sftp> lls

sftp — Batch Mode

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

sftp -b commands.txt admin@server

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

sftp -P 2222 admin@server

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

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

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

  • ssh – remote shell over SSH – the transport scp is built on
  • ssh-keygen – generate an SSH key pair for passwordless scp transfers
  • mosh – resilient SSH alternative for unreliable connections