SCP is a secure copy program in Linux copies files and directories between hosts on the network. It uses SSH to transfer the data. Same as SSH, scp uses the port 22 by default. It provides the same security and uses the same authentication as required by SSH.
SSH is the secure shell program, which can be used for remote system access and it is called secure because SSH sends encrypted data over the network. A system can be accessed using SSH as if it were present on the local machine.
This article provides scp examples for transferring files and directories using this protocol.
Table of Contents
1) Copy a file from Remote host to Local
The following command will copy the file named ‘file_for_copy.txt' from 192.168.1.10 to the current directory.
$ scp [email protected]:/home/bobby/file_for_copy.txt . [email protected]'s password: file_for_copy.txt 100% 0 0.0KB/s 00:00
[email protected]:~$ ls file_for_copy.txt
2) Copy a directory from Remote host to local
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 [email protected]:/home/bobby/dir_for_copy . [email protected]'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/directory 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 [email protected]:/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 protected]:/tmp [email protected]'s password: file2.txt 100% 42 0.0KB/s 00:00 file.txt 100% 39 0.0KB/s 00:01
4) Copying files/directories between two remote hosts
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 protected]:/home/bobby/myfile1.txt [email protected]:/tmp
Similarly, directories can be copied between two remote machines with -r option.
$ scp -3 -r [email protected]:/home/bobby/mydir [email protected]:/tmp [email protected]'s password: Could not chdir to home directory /home/bobby: No such file or directory [email protected]'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 as follows.
[[email protected] ~]$ scp –P 2200 -r test_folder [email protected]:/home/test/
6) Copy multiple files using one command
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 [email protected]:/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 protected]:/home/bobby
8) Fast 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 [email protected]:/home/user/file .
scp -c arcfour [email protected]:/home/user/file .
9) Scp 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 [email protected]:/home/user/file .
10) Scp with limited bandwidth
When using the scp command you can limit your bandwidth as below. (In here bandwidth is specified in Kbit/s)
scp -l50 [email protected]:/home/user/file .
11) Scp with IPV4 and IPV6
If needed, you can force the scp command to use IPV4 or IPV6 as below
scp -4 [email protected]:/home/user/file .
scp -6 [email protected]:/home/user/file .
Thanks for reading and let me know if like to add more scp examples that I miss. Refer scp command man page for more information.