How to Use scp command to Transfer Files

scp examples linux

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.

1) Copy a file from Remote host to Local

The following command will copy the file named ‘file_for_copy.txt' from to the current directory.

$ scp bobby@ .
bobby@'s password:
file_for_copy.txt 100% 0 0.0KB/s 00:00
bobby@Inspiron-1440:~$ ls

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 bobby@ .
bobby@'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 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 bobby@
bobby@'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 to /tmp directory on machine, with verbose output.

$ scp -3 -v bobby@ bobby@

Similarly, directories can be copied between two remote machines with -r option.

$ scp -3 -r bobby@ bobby@
bobby@'s password:
Could not chdir to home directory /home/bobby: No such file or directory
bobby@'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.

[test@clientserver ~]$ scp –P 2200 -r test_folder 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 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 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 bobby@ .
scp -c arcfour bobby@ .

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 bobby@ .

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 bobby@ .

11) Scp with IPV4 and IPV6

If needed, you can force the scp command to use IPV4 or IPV6 as below

scp -4 bobby@ .
scp -6 bobby@ .

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.

Read Also:

Bobbin Zachariah 10:52 pm


Your email address will not be published. Required fields are marked *

All comments are subject to moderation.