SCP is a secure copy program to transfer files or directories between Linux hosts on the network. SCP uses ssh protocol to transfer the data.
You can transfer file or directories as follows:
1) Local host to a remote host.
2) Remote system to your local system.
3) Transfer between two remote host from your local system.
In this tutorial, let's learn some scp commands to transfer files between Linux systems.
Table of Contents
1) Copy a file from Remote host to Local using scp command
The following example will copy the file named ‘file_for_copy.txt' from remote system '192.168.1.10' to the current directory (dot indicate current directory) on the local system.
$ scp firstname.lastname@example.org:/home/bobby/file_for_copy.txt . email@example.com's password: file_for_copy.txt 100% 0 0.0KB/s 00:00
bobby@Inspiron-1440:~$ ls file_for_copy.txt
Copy a directory from Remote host to local using scp command
Directories can be copied with
-r option (like most commands like cp), i.e. recursively, so all the content inside the directory can be copied too
$ scp -r firstname.lastname@example.org:/home/bobby/dir_for_copy . email@example.com's password: file1.txt 100% 0 0.0KB/s 00:00 file3.txt 100% 0 0.0KB/s 00:00 file2.txt 100% 0 0.0KB/s 00:00
$ ls -ld dir_for_copy drwxrwxr-x 2 bobby bobby 4096 2012-11-07 15:11 dir_for_copy
3) Copy a file from Local to Remote host
The -v option of scp prints verbose output, which then performs the scp and also prints debugging messages on their progress. In the following example, -v option is used. Here, a local file is copied to the remote host.
$ scp -v examples.desktop firstname.lastname@example.org:/home/bobby
Similarly, a directory can be copied with
-r option. In the following example, a directory named jvm is copied to the remote host.
$ scp -r jvm email@example.com:/tmp firstname.lastname@example.org's password: file2.txt 100% 42 0.0KB/s 00:00 file.txt 100% 39 0.0KB/s 00:01
4) Copying files between two remote hosts from local host
To copy a file between two hosts, ssh service should be running on both the machines. The
-3 option will transfer files between two remote hosts via the local host. The following command copies the file /home/bobby/myfile1.txt from the machine 192.168.1.10 to /tmp directory on 192.168.1.20 machine, with verbose output.
$ scp -3 -v email@example.com:/home/bobby/myfile1.txt firstname.lastname@example.org:/tmp
Similarly, directories can be copied between two remote machines with -r option.
$ scp -3 -r email@example.com:/home/bobby/mydir firstname.lastname@example.org:/tmp email@example.com's password: Could not chdir to home directory /home/bobby: No such file or directory firstname.lastname@example.org's password:
5) SCP with a Port Number
Suppose, the SSH port of the remote machine is 2200 (rather than the default SSH port 22). Then you need to execute the scp command with
-P option as follows:
$ scp –P 2200 -r test_folder email@example.com:/home/test/
6) Scp Command Copy multiple files (one liner)
You can copy multiple files at once using the scp command as below without having to execute it several times
$ scp file1.txt file2.txt file3.txt firstname.lastname@example.org:/home/bobby
7) Copy files without changing the timestamp of the files
Sometime you may want to preserve the timestamp of the original files. But when you scp them to a different location in the normal way, the timestamp modifies. But with
-p option you can copy the files with the same original timestamps, as below.
$ scp -p file1.txt email@example.com:/home/bobby
8) Fast SCP copy files
By default Scp use AES-128 to encrypt data as the security of this is high. But the downside of this is it slow down the file copy process a bit. But if you want to securely copy files faster, you can use Blowfish or RC4 as below.
$ scp -c blowfish firstname.lastname@example.org:/home/user/file .
$ scp -c arcfour email@example.com:/home/user/file .
9) Scp Command with increased security
If it the security you look for, you can copy files in a more secure way using 3DES. But this will slow down the process a bit more.
$ scp -c 3des firstname.lastname@example.org:/home/user/file .
10) Scp Command with limited bandwidth
When using the scp command you can limit your bandwidth as below (here bandwidth is specified in Kbit/s).
$ scp -l50 email@example.com:/home/user/file .
11) Scp Command with IPV4 and IPV6
If needed, you can force the scp command to use IPV4 or IPV6 as below
$ scp -4 firstname.lastname@example.org:/home/user/file .
$ scp -6 email@example.com:/home/user/file .
I hope you enjoyed reading this tutorial on how to use scp command with examples. I would recommend referring scp command man page for more information.