scp Command Examples
You are probably already familiar with the cp command in Linux which is used to copy files in Linux. cp command can be used only on the local machine.
But what about copying files from a remote Linux system? To copy files from a remote server or to a remote server, you can use a command called scp.
Trivia: scp is short form for Secured Copy.
scp command syntax
The syntax and the usage of scp command is similar to the cp command and you’ll see it shortly in these scp command examples.
But first let’s take a look at the syntax of scp command:
scp <options> source_path destination_path
Depending on the origin of the file to be copied, the source can either be client or server. If the source is the client, then the destination is the server and vice-versa.
But that sounds too simple, isn’t it? The main focus of the scp command is n using the correct format for the source or destination path format.
A typical path to file or a directory on the server is represented in this format:
<username>@<ip_address_of_system>:<path_to_the file/directory>
Actually, you can also use user@hostname in the scp command but it is more likely that you’ll end up with an error “Could not resolve hostname“.
This is because correlating the hostname to the IP address is dependent on the network configuration of the server’s network. Using the actual IP address saves you the trouble.
Things to keep in mind before using scp command
Here are a couple of things you need before you’ll be able to use the scp command on your Linux system:
- Make sure that ssh is installed and running on both client and server machines as scp command internally uses ssh. If ssh is not installed, install it by using the command “sudo apt install ssh” on Debian based machines or using respective package managers of your distro.
- You need to know the IP address of the remote system or the hostname (if it's one the same subnet).
- You need to know the username and password of the remote system. You will be prompted to enter the password of the remote host. Your file transfer will begin only when you enter the correct password of the remote host.
Using scp Command in Linux: 10 Practical Examples
Now that we have seen the syntax of the scp command and format to specify the path to the server, let us now see how to use the scp command.
1. Copying a file to the remote system using scp command
To copy a single file from local machine to remote host, specify the path to the file as source path and path in the remote host where the file has to be copied.
scp abc.txt xyz@<ip_address_of_xyz>:/home/xyz/Desktop
2. Copying a file from the remote system using scp command
Copying a file from remote system to the local system is pretty much the same. You just need to specify the complete path to the file on the remote system and path on the local system.
So to copy file from remote system to the current directory, simply use the command in the following fashion:
scp xyz@<ip_address_of_xyz>:/remote/user/home/abc.txt .
3. Copying multiple files using scp command
To copy multiple files from the local machine to host, just specify the name of the files as the source path.
scp abc.txt def.txt xyz@<ip_address_of_xyz>:/home/xyz/Desktop
4. Copying an entire directory with scp command
Copying a directory using scp is also the same as the cp command. You can use the -r option and specify the name of the folder as the source path. This is called the recursive mode.
scp -r ~/Desktop/test xyz@<ip_address_of_xyz>:/home/xyz/Desktop
You can also specify more than one directory and copy them all in one single command.
5. See the file transfer details in the output
If you are curious to know what happens behind the scenes while copying, you can use -v option to see all the processes that are executed including debugging, exit status, encryption, decryption etc. It’s called the verbose mode.
scp -v abc.txt xyz@<ip_address_of_xyz>:/home/xyz/Desktop
6. Copying files across two remote hosts
So far, you have seen how to copy from local machine to a remote machine.
To copy files from one remote computer to another remote computer, specify the paths of both source and destination in remote computer’s format as discussed above.
scp abc@<ip_address_of_abc>:<path_of_file_or_folder> xyz@<ip_address_of_xyz>:<path_of_file_or_folder
Needless to say that you need to know password of both the systems here.
7. Copying files with compression [reduces transfer time]
If you try to copy a bunch of files together, it will take more time than the transfer of a single file of the size equivalent to the sum of all the individual files.
If you feel that you need to increase the speed of transferring files, you can compress the files by using -C option and transfer the files.
The best thing is that the compression happens only while transferring. Once the transfer is completed, the files will be stored in their original form. You won’t see an arhcive file but all the individual files as they were on the remote server.
scp -C abc.txt xyz@<ip_address_of_xyz>:/home/xyz/Desktop
8. Limiting the bandwidth of file transfer
If you feel that transfer of files is utilizing most of the bandwidth, you can reduce the bandwidth of file transfer using -l option followed by the new bandwidth rate.
scp -l 800 abc.txt xyz@<ip_address_of_xyz>:/home/xyz/Desktop
Note that the bandwidth you specify in the command is in kilobits per sec but while transferring the files, the scp command output will show the transfer rate in kilobytes per second.
Since 1 byte=8 bits, the8300 kilobits per second in the command parameter becomes 100 kilobytes per soond in the command output.
9. Preserving the original attributes of the copied files
If you need to preserve the original file attributes such as the file permissions and ownership, file timestamps, etc., use -p option.
scp -p abc.txt xyz@<ip_address_of_xyz>:/home/xyz/Desktop
10. Hiding the output of scp command
If you are not comfortable with the lengthy output of scp command, you can avoid the output of the command by using -q option. This is called the quiet mode.
It even hides the progress of copying files.
scp -q abc.txt xyz@<ip_address_of_xyz>:/home/xyz/Desktop
In the above examples, you have seen how to copy the files to the remote host from the local machine. If you need to copy files from remote host to the local machine, write the path of the file/folder to be copied from the remote host as source path and the path where the file/folder has to be pasted in the local machine as destination path.
Conclusion
For more detail on the scp command, please refer to its man page.
I hope that you now understand how to make the best use of scp command to copy files between computers securely. Personally, I prefer using rsync command over scp because rsync has more features.
However, like the docker cp command, scp also follows a similar syntax to the common cp command. And that makes it an attractive choice for many Linux users.
If you come across any issues feel free to ask questions in the comment section below. And if you know any other awesome scp command tip, do share it with the rest of the us here.