Scp Command in Unix

SCP (Secure Copy) is a command-line tool to copy or transfer files across hosts. Use the same type of security mechanisms such as the ssh program. Infact connection using ssh in the background to do the file transfer. SCP refer either to "protocols" define how secure your copy should work and "program" (command) that are installed as part of a package of OpenSSH tools.

In this quick tutorial we'll look at some examples of the command scp and how it can be used to transfer files securely.

Install the scp
SCP is generally installed by default on most linux distros as part of package openssh. For example, Ubuntu/Debian openssh-client package provides the scp program.
$ dpkg -L openssh-klien | grep scp /usr/bin/scp /usr/share/man/man1/scp.1.gz

With [] package provides the ssh, SCP, SFTP program along with many other tools. So we don't have to do anything extra here, except for using and learning programs.

Using scp
The basic syntax of scp is very simple to memorize. It looks like this
$ SCP source_file_path destination_file_path

Depending on the host, the file path must include the full host address, port number, username and password with the directory path.

So, if you are "shipping" file from your local computer to a remote machine (upload) the syntax would look like this.
$ SCP ~/my_local_file.txt / some/remote/directory

When copying a file from the remote host to the local host (download), which looks the opposite
$ SCP / some/remote/directory ~/my_local_file.txt
just downloaded file
$ SCP user@ .

That's pretty much about using scp for routine tasks. In addition, there are several additional options and functions that support the scp. Let's take a brief overview of them.

And Yes, by default the scp will always overwrite the file at the destination. If you need to avoid that, use a more powerful tool called rsync.

Verbose output
With verbose output, the scp program will output a lot of information about what it does in the background. This is often useful when the program fails to or cannot complete the request. Verbose output will then indicate the exact point at which the program ran into problems.

Transfer files
Multiple files can be specified with a separated by a space like this
$ SCP foo.txt bar.txt username@remotehost:/path/directory/

To copy a file from the remote host to the local current directory
$ SCP username@remotehost:/path/directory/{foo.txt,bar.txt}
$ SCP root@ ~ / {abc.log,cde.txt}

Copy the entire directory (recursively)
To copy the entire directory from one host to another use of r switch and specify the directory
$ SCP - v - r ~/Downloads root@ / root/week

Copy the files across 2 remote host
SCP can copy files from a remote host 1 to host other remote too.
$ SCP user1@remotehost1:/some/remote/dir/foobar.txt user2@remotehost2:/some/remote/dir/

Accelerating the transfer with compression
The super cool options to speed up the transfer to save time and bandwidth. All you need to do is use the C option to enable compression. Files that are compressed with quickly and compressed on the goal.
$ SCP - vrC ~/Downloads root@ / root/week

In the example above we moved the entire directory with compression enabled. Gain the speed will depend on how many files can be compressed.

Limit bandwidth usage
If you don't want the scp to take the entire available bandwidth, then use the l option to limit maximum speed Kbit/s.
$ SCP - vrC -l 400 ~/Downloads root@ / root/week

Connect to a different port number of the remote host
If the remote server has ssh daemon running on another port (default is 22), then you need to tell the scp to use specific port numbers using the '-P ' option.
$ SCP -vC -P 2200 ~/Test.txt root@

Preserve file attributes
'-P ' option (smallcase), this will preserve modification times, access times, and the mode of the original file.
$ SCP - C -p ~/test.txt root@

Quiet mode
In quiet mode ('-q ' option), scp output will get supressed, and will disable the progress meter as well as a warning and diagnostic messages.
$ SCP - vCq ~/test.txt root@

Determine the identity of the file
When using key based authentication (passwordless), you will need to determine the identity of the file containing the private key. This option is directly passed to ssh commands and work the same way.
$ SCP - vCq -i private_key.pem ~/test.txt root@

Use different files ssh_config
The use of the '-F ' option to specify a different file in ssh_config.
$ SCP -vC -F /home/user/my_ssh_config ~/test.txt root@

Use a different password
SCP by default using the cipher or AES encryption. Sometimes you may want to use a different password. Use a different password can accelerate the transfer process. For example blowfish and arcfour known as faster than AES (but less secure).
$ SCP - c blowfish - C ~/local_file.txt username@remotehost:/remote/path/file.txt

In the example above we use blowfish cipher together with compression. This can provide a significant speed boost depending on the available bandwidth.

Although the scp secure transfer files efficiently, it lacks the features of file synchronization tool. All it can do is copy paste all files referred from one location to another.

A more powerful tool is Rsync that not only has all the functions of the scp but add more intelligently synchronize files on host 2. For example, can check and upload only files modified, Skip existing files and so on.

Click here for Comments