How to Use SCP Command for A File/Directory Transfer in Linux

scp examples linux

SCP is a secure copy program in Linux copies files/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

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/raghu/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 the remote host

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/raghu/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 raghu raghu 4096 2012-11-07 15:11 dir_for_copy

3) Copy a file/directory to the 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/raghu

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/raghu/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/raghu/myfile1.txt [email protected]:/tmp

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

$ scp -3 -r [email protected]:/home/raghu/mydir [email protected]:/tmp
[email protected]'s password:
Could not chdir to home directory /home/raghu: 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/raghu

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/raghu

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 :-)

Read Also:

Bobbin Zachariah 10:52 pm

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.

Comments

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

All comments are subject to moderation.