
scp Command in Linux
Securely transferring files between systems is a common requirement in Linux environments. The scp (secure copy) command is an essential tool for this purpose, utilizing SSH (Secure Shell) to securely transfer data.
The scp (secure copy) command is a straightforward yet powerful utility for copying files and directories between different hosts over a secure SSH connection. By using SSH, scp ensures that your data is encrypted during transfer, providing a secure way to handle file copying operations across the network.
Mastering scp allows you to efficiently and securely move data between systems, enhancing your workflow and data management.
Table of Contents
Here is a comprehensive guide to the options available with the scp command â
Syntax of the scp Command
The general syntax for the scp command is −
scp [options] source destination
In this command, source specifies the file or directory to be copied, and destination indicates where it should be copied to.
scp Command Options
The scp command offers several options to customize its behavior −
Option | Description |
---|---|
-r | Recursively copy entire directories, including all files and subdirectories. |
-p | Preserves the file attributes such as modification times, access times, and modes from the original file. |
-q | Disables the progress meter and non-error messages. |
-C | Enables compression, which can speed up file transfers, especially over slow connections. |
-i identity_file | Specifies the private key file for authentication, useful if you need to use a specific key rather than the default SSH key. |
-P port | Specifies the port number to connect to on the remote host, if itâs different from the default SSH port (22). |
-l limit | Limits the bandwidth used by the scp transfer, specified in Kbit/s. |
Examples of scp Command in Linux
Here are some practical scenarios where scp can be effectively used −
- Copying a File to a Remote System
- Copying a File from Remote System
- Recursively Copying a Directory to a Remote Host
- Using a SSH Key for Authentication
- Limiting Bandwidth During Transfer
Copying a File to a Remote System
Imagine you need to move a file from your local computer to a remote server. You can achieve this with the following command −
scp localfile.txt user@remotehost:/path/to/remote/directory
This command transfers localfile.txt from your local machine to the specified directory on the remote host, ensuring the file is securely copied over the network.

Copying a File from a Remote System
Suppose you need to retrieve a file from a remote server to your local machine. You can use the following scp command −
scp user@remotehost:/path/to/remotefile.txt /local/directory
This command fetches remotefile.txt from the remote server and saves it in the specified local directory, allowing you to access the file on your local system.

Recursively Copying a Directory to a Remote Host
If you need to transfer an entire directory, including its subdirectories and files, to a remote server, use the -r option −
scp -r /local/directory user@remotehost:/path/to/remote/directory
This command recursively copies all contents of /local/directory to the remote directory, maintaining the directory structure during the transfer.
Using a Custom SSH Key for Authentication
To use a specific SSH key for authentication while transferring files, specify the key with the -i option −
scp -i /path/to/privatekey.pem localfile.txt user@remotehost:/path/to/remote/directory
This command utilizes the provided private key file for secure authentication and transfers localfile.txt to the remote server.
Limiting Bandwidth During Transfer
To control the bandwidth used during the file transfer, the -l option can be helpful −
scp -l 500 localfile.txt user@remotehost:/path/to/remote/directory
This command limits the transfer speed to 500 Kbit/s, ensuring the file is copied to the remote host while managing network bandwidth usage efficiently.

Conclusion
The scp (secure copy) command is a useful tool for securely transferring files and directories between different systems within a Linux environment. By leveraging SSH (Secure Shell), scp ensures that your data remains encrypted and protected throughout the transfer process. Understanding the purpose, syntax, options, and practical usage examples of scp empowers you to efficiently manage file transfers, maintain data integrity, and enhance overall workflow.
Whether you need to copy individual files to a remote server, fetch data from a remote system, transfer entire directories, utilize custom SSH keys for authentication, or control bandwidth usage, mastering scp offers a versatile and secure solution.