SCP or Secure Copy securely transfers files between two hosts over the network using the SSH protocol. This tutorial explains how to use this popular command with numerous examples.
SCP is pre-installed with most Linux distributions and is often used for deploying software to servers and backing up – frequently automated using Bash Scripts.
SCP Command Syntax
scp OPTIONS SOURCE ... TARGET
SCP has a lot of options that are outlined on the commands manual, available by running:
man scp
Here’s a summary of the most commonly used options from the manual:
– B | Selects batch mode (prevents asking for passwords or passphrases). |
– C | Compression enable. Passes the -C flag to ssh to enable compression. |
– i identity_file | Selects the file from which the identity (private key) for public-key authentication is read. This option is directly passed to ssh. |
– l limit | Limits the used bandwidth, specified in Kbit/s. |
– P port | Specifies the port to connect to on the Remote Host. Note that this option is written with a capital ‘P’ because -p is already reserved for preserving the times and modes of the file. |
– p | Preserves modification times, access times, and modes from the original file. |
– q | Quiet mode: disables the progress meter as well as warning and diagnostic messages from ssh. |
– r | Recursively copy entire directories. Note that SCP follows symbolic links encountered in the tree traversal. |
– v | Verbose mode. Causes SCP and ssh to print debugging messages about their progress. This helps debug connection, authentication, and configuration problems. |
SCP Command Examples
Copy File from Local Host -> Remote Host
scp my_file.txt user@destination_host:/path/to/remote/directory/
Note that:
- user is the username of the user you wish to log in as on the destination_host
- You may be prompted for credentials
Copy File from Remote Host -> Local Host with Compression
scp -C user@sending_host:my_file.txt /path/to/local/directory/
Copy Directory from Remote Host -> Local Host
scp -r user@sending_host:/path/to/remote/directory/ /path/to/local/directory/
Copy Directory from Local Host -> Remote Host with Bandwidth Limit
scp -l 512 -r /path/to/local/directory/ user@destination_host:/path/to/remote/directory/
Copy File from Remote Host -> Remote Host with SSH Authentication Key
scp -i /path/to/ssh/key user@sending_host:/path/to/remote/directory/my_file.txt user@destination_host:/path/to/remote/directory/
Conclusion
When developing PHP, JavaScript, and other web applications, you will want to develop on your local computer and transfer files to your public server. SCP is one way to do this.
Please take a look at our other networking articles.