Table of Contents
SCP stands for Secure Copy, and it is a command-line tool that allows you to securely transfer files between hosts using the SSH protocol. SCP is useful for many scenarios, such as backing up data, deploying applications, synchronizing directories, and more.
In this article, you will learn everything you need to know about how to use SCP command in Linux, including its syntax, options, examples, tips and tricks, and alternatives. By the end of this article, you will be able to use SCP confidently and efficiently for your file transfer needs.
SCP Syntax and Options
The basic syntax of SCP is as follows:
scp [options] source destination
The source and destination can be either local or remote hosts, and they are specified in the following format:
[user@]host:[path]
If the user or host is omitted, it defaults to the current user or host. If the path is omitted, it defaults to the home directory of the user. The colon (:) is required to separate the host and the path.
The options are optional flags that modify the behavior of SCP. Some of the common options are:
-P port
: Specifies the port number to use for SSH connection. The default port is 22.-p
: Preserves the permissions and timestamps of the files.-r
: Recursively copies directories and their contents.-v
: Enables verbose mode, which displays detailed information about the transfer process.-C
: Enables compression, which reduces the bandwidth usage and speeds up the transfer.-i identity_file
: Specifies the identity file (private key) to use for authentication.
You can use SCP to copy files between local and remote hosts, or between two remote hosts. The following sections will show you some examples of how to do that.
SCP Examples
How to copy a single file from local to remote host?
To copy a single file from your local machine to a remote host, you can use the following command:
scp file.txt user@remote_host:/path/to/destination
This will copy the file file.txt
from your current directory on your local machine to the /path/to/destination
directory on the remote host. You will be prompted to enter the password of the user on the remote host.
How to copy a single file from remote to local host?
To copy a single file from a remote host to your local machine, you can use the following command:
scp user@remote_host:/path/to/file.txt /path/to/destination
This will copy the file file.txt
from the /path/to
directory on the remote host to the /path/to/destination
directory on your local machine. You will be prompted to enter the password of the user on the remote host.
How to copy multiple files from local to remote host?
To copy multiple files from your local machine to a remote host, you can use the following command:
scp file1.txt file2.txt user@remote_host:/path/to/destination
This will copy the files file1.txt
and file2.txt
from your current directory on your local machine to the /path/to/destination
directory on the remote host. You will be prompted to enter the password of the user on the remote host.
You can also use wildcards (*) to match multiple files with a common pattern. For example:
scp *.txt user@remote_host:/path/to/destination
This will copy all files with .txt
extension from your current directory on your local machine to the /path/to/destination
directory on the remote host.
How to copy multiple files from remote to local host?
To copy multiple files from a remote host to your local machine, you can use the following command:
scp user@remote_host:/path/to/file1.txt /path/to/file2.txt /path/to/destination
This will copy the files file1.txt
and file2.txt
from the /path/to
directory on the remote host to the /path/to/destination
directory on your local machine. You will be prompted to enter the password of the user on the remote host.
You can also use wildcards (*) to match multiple files with a common pattern. For example:
scp user@remote_host:/path/to/*.txt /path/to/destination
This will copy all files with .txt
extension from the /path/to
directory on the remote host to the /path/to/destination
directory on your local machine.
How to recursively copy directories from local to remote host?
To recursively copy directories and their contents from your local machine to a remote host, you can use the -r
option. For example:
scp -r dir user@remote_host:/path/to/destination
This will copy the directory dir
and all its subdirectories and files from your current directory on your local machine to the /path/to/destination
directory on the remote host. You will be prompted to enter the password of the user on the remote host.
How to recursively copy directories from remote to local host?
To recursively copy directories and their contents from a remote host to your local machine, you can use the -r
option. For example:
scp -r user@remote_host:/path/to/dir /path/to/destination
This will copy the directory dir
and all its subdirectories and files from the /path/to
directory on the remote host to the /path/to/destination
directory on your local machine. You will be prompted to enter the password of the user on the remote host.
SCP Tips and Tricks
Here are some tips and tricks that can help you use SCP more effectively and efficiently.
How to use SCP with a non-standard port?
If the SSH server on the remote host is listening on a non-standard port, you can use the -P
option to specify the port number. For example:
scp -P 2222 file.txt user@remote_host:/path/to/destination
This will copy the file file.txt
from your current directory on your local machine to the /path/to/destination
directory on the remote host using port 2222 for SSH connection.
How to use SCP with a passwordless SSH key?
If you have set up a passwordless SSH key for authentication, you can use the -i
option to specify the identity file (private key) to use. For example:
scp -i ~/.ssh/id_rsa file.txt user@remote_host:/path/to/destination
This will copy the file file.txt
from your current directory on your local machine to the /path/to/destination
directory on the remote host using the private key ~/.ssh/id_rsa
for authentication. You will not be prompted to enter the password of the user on the remote host.
How to use SCP with compression and encryption?
If you want to reduce the bandwidth usage and speed up the transfer, you can use the -C
option to enable compression. This will compress the data before sending it over the network and decompress it after receiving it. For example:
scp -C file.txt user@remote_host:/path/to/destination
This will copy the file file.txt
from your current directory on your local machine to the /path/to/destination
directory on the remote host using compression.
If you want to increase the security and privacy of your data, you can use SSH options to specify encryption algorithms. For example:
scp -o Cipher=aes256-ctr file.txt user@remote_host:/path/to/destination
This will copy the file file.txt
from your current directory on your local machine to the /path/to/destination
directory on the remote host using AES-256-CTR encryption algorithm.
You can also combine compression and encryption options for better performance and security. For example:
scp -C -o Cipher=aes256-ctr file.txt user@remote_host:/path/to/destination
This will copy the file file.txt
from your current directory on your local machine to the /path/to/destination
directory on the remote host using compression and AES-256-CTR encryption algorithm.
How to use SCP with verbose mode and progress bar?
If you want to see more information about what SCP is doing, you can use the -v
option to enable verbose mode. This will display detailed information about the transfer process, such as connection establishment, authentication, file transfer, etc. For example:
scp -v file.txt user@remote_host:/path/to/destination
This will copy the file file.txt
from your current directory on your local machine to the /path/to/destination
directory on the remote host in verbose mode.
If you want to see how much data has been transferred and how fast it is going, you can use a third-party tool called pv (pipe viewer) to add a progress bar. For example:
pv file.txt | scp - user@remote_host:/path/to/destination/file.txt
This will copy the file file.txt
from your current directory on your local machine to the /path/to/destination/file.txt
file on the remote host with a progress bar showing how much data has been copied.
SCP Alternatives
SCP is not the only tool that can perform file or directory transfer over a network. There are some other tools that can do similar tasks as SCP, but with different features, speed, and security. Here are some of the most popular ones:
Rsync
Rsync is a tool that can synchronize files and directories between local and remote hosts, or between two remote hosts. Rsync can detect and transfer only the changes in the files or directories, which makes it faster and more efficient than SCP.
Rsync can also preserve more attributes of the files or directories, such as symbolic links, hard links, and ACLs. Rsync can use SSH as the underlying protocol for encryption and authentication, or use its own protocol (rsyncd) for faster transfer.
SFTP
SFTP stands for Secure File Transfer Protocol, which is an extension of SSH that provides file transfer and manipulation capabilities. SFTP is more interactive than SCP, which means that you can browse, create, delete, rename, and modify files or directories on both local and remote hosts using commands or graphical interfaces.
SFTP also supports more features than SCP, such as resuming interrupted transfers, transferring multiple files at once, and setting file permissions.
FTP
FTP stands for File Transfer Protocol, which is one of the oldest and most widely used protocols for file transfer over a network. FTP is simple and easy to use, but it has some drawbacks compared to SCP. FTP does not provide encryption or authentication by default, which means that your data and credentials are exposed to anyone who can intercept the network traffic.
FTP also does not preserve the permissions or timestamps of the files or directories transferred. FTP can be secured by using SSL/TLS (FTPS) or SSH (FTP over SSH), but these options are not supported by all FTP servers or clients.
Here is a table showing the pros and cons of each tool:
Tool | Pros | Cons |
---|---|---|
SCP | Secure and encrypted by default | Slow and inefficient for large or incremental transfers |
Rsync | Fast and efficient for large or incremental transfers | Complex and verbose syntax |
SFTP | Interactive and feature-rich | Not compatible with SCP syntax |
FTP | Simple and easy to use | Insecure and unencrypted by default |
Conclusion
In this article, we have learned how to use SCP command in Linux to copy files or directories over a network using SSH encryption. We have seen how to write a basic SCP command and what are the options and flags that we can use to modify its behavior. We have also seen some examples of how to use SCP in different scenarios and situations.
We hope that this article has helped you understand how to use SCP command in Linux and everything you need to know about it. If you have any questions or feedback, please let us know in the comments below. Thank you for reading!