Site icon DesignLinux

10 SCP Commands to Transfer Files/Folders in Linux

Linux administrators should be familiar with the CLI environment. Since GUI mode in Linux servers is not common to be installed. SSH may be the most popular protocol to enable Linux administrators to manage the servers via remote secure way. Built-in with SSH command there is SCP command. SCP is used to copy file(s) between servers in a secure way.

[ You might also like: How to Secure and Harden OpenSSH Server ]

10 Linux SCP Commands

Basic Syntax of SCP Command

The below command will read as “copy source_file_name” into “destination_folder” at “destination_host” using “username account”.

scp source_file_name [email protected]_host:destination_folder

There are many parameters in the SCP command that you can use. Here are the parameters that may use on daily basis usage.

Provide the detailed information of the SCP process using the -v parameter

The basic SCP command without parameters will copy the files in the background. Users will see nothing unless the process is done or some error appears.

You can use the “-v” parameter to print debug information into the screen. It can help you debugging connection, authentication, and configuration problems.

[email protected] ~/Documents $ scp -v Label.pdf [email protected]@202.x.x.x:.
Sample Output
SCP Show Progress While Copying Files
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -t .
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: Host '202.x.x.x' is known and matches the RSA host key.
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Next authentication method: password
[email protected]'s password:
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
Sending file modes: C0770 3760348 Label.pdf
Sink: C0770 3760348 Label.pdf
Label.pdf 100% 3672KB 136.0KB/s 00:27
Transferred: sent 3766304, received 3000 bytes, in 65.2 seconds
Bytes per second: sent 57766.4, received 46.0
debug1: Exit status 0

Provide modification times, access times, and modes from original files

The “-p” parameter will help you with this. An estimated time and the connection speed will appear on the screen.

[email protected] ~/Documents $ scp -p Label.pdf [email protected]:.
Sample Output
SCP Estimating the Time Needed to Copy Large File
[email protected]'s password:
Label.pdf 100% 3672KB 126.6KB/s 00:29

Make file transfer faster using -C parameter

One of the parameters that can faster your file transfer is the “-C” parameter. The “-C” parameter will compress your files on the go. The unique thing is the compression-only happens in the network. When the file has arrived at the destination server, it will be returning to the original size as before the compression happen.

Take a look at these commands. It is using a single file of 93 Mb.

[email protected] ~/Documents $ scp -pv messages.log [email protected]:.
Sample Output
SCP Transfers File Without Compression
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t .
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: identity file /home/pungki/.ssh/id_rsa type -1
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Trying private key: /home/pungki/.ssh/id_rsa
debug1: Next authentication method: password
[email protected]'s password:
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
debug1: Sending command: scp -v -p -t .
File mtime 1323853868 atime 1380425711
Sending file timestamps: T1323853868 0 1380425711 0
messages.log 100% 93MB 58.6KB/s 27:05
Transferred: sent 97614832, received 25976 bytes, in 1661.3 seconds
Bytes per second: sent 58758.4, received 15.6
debug1: Exit status 0

Copying files without the “-C” parameter will result in 1661.3 seconds. You may compare the result to the command below which using the “-C” parameter.

[email protected] ~/Documents $ scp -Cpv messages.log [email protected]:.
Sample Output
SCP Transfers File Faster Using Compression
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t .
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: identity file /home/pungki/.ssh/id_rsa type -1
debug1: Host '202.x.x.x' is known and matches the RSA host key.
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Next authentication method: publickey
debug1: Trying private key: /home/pungki/.ssh/id_rsa
debug1: Next authentication method: password
[email protected]'s password:
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
debug1: channel 0: new [client-session]
debug1: Sending command: scp -v -p -t .
File mtime 1323853868 atime 1380428748
Sending file timestamps: T1323853868 0 1380428748 0
Sink: T1323853868 0 1380428748 0
Sending file modes: C0600 97517300 messages.log
messages.log 100% 93MB 602.7KB/s 02:38
Transferred: sent 8905840, received 15768 bytes, in 162.5 seconds
Bytes per second: sent 54813.9, received 97.0
debug1: Exit status 0
debug1: compress outgoing: raw data 97571111, compressed 8806191, factor 0.09
debug1: compress incoming: raw data 7885, compressed 3821, factor 0.48

As you can see, when you are using compression, the transfer process is done in 162.5 seconds. It is 10 times faster than not using the “-C” parameter. If you are copying a lot of files across the network, the “-C” parameter would help you to decrease the total time you need.

The thing that we should notice is that the compression method will not work on any files. When the source file is already compressed, you will not find any improvement there. Files such as .zip, .rar, pictures, and .iso files will not be affected by the “-C” parameter.

Change SCP Cipher to Encrypt Files

By default SCP using “AES-128” to encrypt files. If you want to change to another cipher to encrypt it, you can use the “-c” parameter. Take a look at this command.

[email protected] ~/Documents $ scp -c 3des Label.pdf [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 282.5KB/s 00:13

The above command tells SCP to use the 3des algorithm to encrypt the file. Please be careful that this parameter using “-c” not “-C“.

Limiting Bandwidth Usage with SCP Command

Another parameter that may be useful is the “-l” parameter. The “-l” parameter will limit the bandwidth to use. It will be useful if you do an automation script to copy a lot of files, but you don’t want the bandwidth is drained by the SCP process.

[email protected] ~/Documents $ scp -l 400 Label.pdf [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 50.3KB/s 01:13

The 400 value behind the “-l” parameter is mean that we limit the bandwidth for the SCP process to only 50 KB/sec. One thing to remember is that bandwidth is specified in Kilobits/sec (kbps). It is mean that 8 bits are equal to 1 byte.

While SCP counts in Kilobyte/sec (KB/s). So if you want to limit your bandwidth for SCP maximum of only 50 KB/s, you need to set it into 50 x 8 = 400.

Specify the Specific port to use with SCP

Usually, SCP is using port 22 as a default port. But for security reasons, you may change the port into another port. For example, we are using port 2249. Then the command should be like this.

[email protected] ~/Documents $ scp -P 2249 Label.pdf [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 262.3KB/s 00:14

Make sure that it uses capital “P” not “p” since “p” is already used for preserved times and modes.

Copy files inside directory recursively

Sometimes we need to copy the directory and all files/directories inside it. It will be better if we can do it in 1 command. SCP supports that scenario using the “-r” parameter.

[email protected] ~/Documents $ scp -r documents [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 282.5KB/s 00:13
scp.txt 100% 10KB 9.8KB/s 00:00

When the copy process is done, at the destination server you will found a directory named “documents” with all its files. The folder “documents” is automatically created.

Disable progress meter and warning / diagnostic message

If you choose not to see progress meter and warning / diagnostic messages from SCP, you may disable it using the “-q” parameter. Here’s the example.

[email protected] ~/Documents $ scp -q Label.pdf [email protected]:.

[email protected]'s password:
[email protected] ~/Documents $

As you can see, after you enter the password, there is no information about the SCP process. After the process is complete, you will see a prompt again.

Copy files using SCP through Proxy

The proxy server is usually used in the office environment. Natively, SCP is not proxy configured. When your environment using a proxy, you have to “tell” SCP to communicate with the proxy.

Here’s the scenario. The proxy address is 10.0.96.6 and the proxy port is 8080. The proxy also implemented user authentication. First, you need to create the “~/.ssh/config” file. Second, you put this command inside it.

ProxyCommand /usr/bin/corkscrew 10.0.96.6 8080 %h %p ~/.ssh/proxyauth

Then you need to create file “~/.ssh/proxyauth” which contains.

myusername:mypassword

After that, you can do SCP transparently as usual.

Please notice that the corkscrew is might not be installed yet on your system. On my Linux Mint, I need to install it first, using the standard Linux Mint installation procedure.

$ apt-get install corkscrew

For other yum-based systems, users can install corkscrew using the following yum command.

# yum install corkscrew

Another thing is that since the “~/.ssh/proxyauth” file contains your “username” and “password” in clear-text format, please make sure that the file can be accessed by you only.

Select different ssh_config file

For mobile users who often switch between the company networks and public networks, it will be suffering to always change settings in SCP. It is better if we can put a different ssh_config file to match our needs.

Here’s a sample scenario

Proxy is used in the company network but not in the public network and you regularly switch networks.

[email protected] ~/Documents $ scp -F /home/pungki/proxy_ssh_config Label.pdf

[email protected]:.
[email protected]'s password:
Label.pdf 100% 3672KB 282.5KB/s 00:13

By default “ssh_config” file per user will be placed in “~/.ssh/config“. Creating a specific “ssh_config” file with proxy compatibility will make it easier to switch between networks.

When you are on the company network, you can use the “-F” parameter. When you are on a public network, you can skip the “-F” parameter.

[ You might also like: Pscp – Transfer/Copy Files to Multiple Linux Servers Using Single Shell ]

That’s all about SCP. You can see man pages of SCP for more detail. Please feel free to leave comments and suggestions.

Exit mobile version