Quick take: The scp command copies files over SSH: scp file user@host:/path to upload, or scp user@host:/path file to download. Add -r for directories and -P for a custom port.
Introduction
The scp command (secure copy) transfers files between machines over the encrypted SSH connection. Because it uses the same authentication as ssh, anything you can log in to, you can copy to and from — making it a quick, secure way to move files to and from servers.
This guide covers uploading and downloading files and directories, custom ports and keys, and when to switch to rsync for larger or repeated transfers.
Syntax
The basic syntax of the scp command is:
scp [OPTIONS] SOURCE DESTINATIONCommon Options and Parameters
The most useful options and parameters for the scp command:
| Option | Description |
|---|---|
| -r | Recursively copy an entire directory. |
| -P PORT | Connect on a custom SSH port (note: capital P). |
| -i KEYFILE | Use a specific private key. |
| -p | Preserve modification times and permissions. |
| -C | Compress data during the transfer. |
| -v | Verbose output for debugging. |
| -3 | Route a remote-to-remote copy through the local machine. |
Practical Examples
Real scp commands you can run today:
# Upload a file to a server
scp backup.tar.gz irfan@server:/home/irfan/
# Download a file from a server
scp irfan@server:/var/log/app.log .
# Copy a whole directory recursively
scp -r ./mysite irfan@server:/var/www/
# Use a custom port
scp -P 2222 file.txt irfan@server:/tmp/
# Use a specific key and preserve attributes
scp -i ~/.ssh/deploy_key -p config.yml deploy@app01:/etc/app/scp vs rsync vs sftp
scp is not the only way to move files over SSH, and choosing the right tool saves time. Each suits a different job:
- scp — simplest for a quick one-off copy of a file or directory. No resume, no incremental sync.
- rsync — best for large transfers and repeated syncs; it copies only changed data, shows progress, and can resume. Use it for backups and deployments.
- sftp — an interactive session for browsing the remote filesystem and transferring files by hand, useful when you are exploring rather than scripting.
A practical rule: reach for scp when you would otherwise drag one file across, and switch to rsync the moment the transfer is large, repeated, or might be interrupted.
Tips and Best Practices
- Note the capital
-Pfor the port in scp —sshuses lowercase-p, a common mix-up. - For large directories or repeated syncs,
rsyncis faster because it only transfers changed data and can resume. - Wrap remote paths in quotes if they contain spaces or wildcards so the shell does not expand them locally.
Final Thoughts
scp is the simplest secure way to move files to and from remote servers, reusing your existing SSH credentials. Remember -r for directories and the capital -P for ports, and step up to rsync when you need speed, resumes, or incremental syncs. It rounds out the SSH toolkit alongside ssh itself.
FAQ: scp Command in Linux
How do I copy a file to a remote server with scp?+
Run scp localfile user@host:/remote/path. To download instead, swap the order: scp user@host:/remote/file localpath.
How do I copy a directory with scp?+
Use the -r flag: scp -r ./mydir user@host:/path copies the directory and everything inside it recursively.
How do I use a custom port with scp?+
Use a capital -P: scp -P 2222 file user@host:/path. This differs from ssh, which uses a lowercase -p for the port.
How do I use an SSH key with scp?+
Add -i with the key path: scp -i ~/.ssh/mykey file user@host:/path uses that private key for authentication.
Should I use scp or rsync?+
scp is fine for one-off copies. rsync is better for large transfers and repeated syncs because it only sends changed data, shows progress, and can resume interrupted transfers.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support