scp command can be used to copy files from local to remote system in Linux

Published on Aug. 22, 2023, 12:12 p.m.

SCP stands for ‘Secure Copy’. scp is a command-line utility offered by Linux.

Scp can be a very good option to make a secure transfer of the files and directories.

You can heavily rely on the scp command for confidentiality and integrity respectively.

Getting started with scp command .

You can transfer files using the scp command.

scp [Option] [source_file_name] [user@destination_Host]:destination_folder

The colon (:) symbol is used in syntax. It separates between the local and remote locations.We use the colon (:) with the remote system to specify the directory.In case we do not specify the target directory, then the files will be copied to the home directory.

Options used with scp oppress.

Some of the most popular options used with the scp command .

Copying files from local to remote system.

Scp allows you to transfer files from your local system to a remote system .

General Syntax: oppress.

scp [file_name] remote_user@host:[destination_folder]

Let us check the output on the remote system .

root@ubuntu-s-1vcpu-1gb-blr1-01:~/gaurav# ls
apache-tomcat-9.0.8.tar.gz
root@ubuntu-s-1vcpu-1gb-blr1-01:~/gaurav#

Thus, the file is successfully copied to the remote system.

Copying files to a remote system.

容器,拖动内容到这里添加!

General Syntax:

scp [file 1] [file 2] [file n] remote_username@remote_host:[specific directory]

Example:

scp ath.html abc.txt ppa-purge_0.2.8+bzr56_all.deb [email protected]:gaurav

Output:

gaurav@ubuntu:~$ scp ath.html abc.txt ppa-purge_0.2.8+bzr56_all.deb [email protected]:gaurav 
[email protected]'s password: 
ath.html                                      100%  199KB  94.7KB/s   00:02    
abc.txt                                       100%    0     0.0KB/s   00:00    
ppa-purge_0.2.8+bzr56_all.deb                 100% 4360    42.2KB/s   00:00    
gaurav@ubuntu:~$

On remote system:

root@ubuntu-s-1vcpu-1gb-blr1-01:~/gaurav# ls -l
total 9800
-rw-r--r-- 1 root root       0 Oct  5 08:58 abc.txt
-rw-r--r-- 1 root root 9818695 Oct  5 08:35 apache-tomcat-9.0.8.tar.gz
-rw-r--r-- 1 root root  204057 Oct  5 08:58 ath.html
-rw-r--r-- 1 root root    4360 Oct  5 08:58 ppa-purge_0.2.8+bzr56_all.deb
root@ubuntu-s-1vcpu-1gb-blr1-01:~/gaurav#

Copying a directory to a remote system.

The -r option is used to copy directories recursively.That means, all the sub-folder will also be copied.

General syntax:

scp -r [directory path] remote_username@remote_host:[target_directory]

Example:

scp -r PycharmProjects [email protected]:gaurav

Output:

gaurav@ubuntu:~$ scp -r PycharmProjects [email protected]:gaurav
[email protected]'s password: 
__main__.py                                   100%  623     7.8KB/s   00:00    
__init__.py                                   100%   23     0.4KB/s   00:00    
completion.py                                 100% 2929    28.1KB/s   00:00    
search.py                                     100% 4728    38.7KB/s   00:00    
uninstall.py                                  100% 2963    32.5KB/s   00:00    
hash.py                                       100% 1681    21.3KB/s   00:00    
check.py                                      100% 1430    16.8KB/s   00:00    
configuration.py                              100% 7125    50.4KB/s   00:00    
show.py                                       100% 6289    49.8KB/s   00:00    
download.py                                   100% 6623    48.2KB/s   00:00  
gaurav@ubuntu:~$  

Displaying detailed information about the .

You can use the -v (lowercase v) option to display the information about the files that are being copied on.

General Syntax:

scp -v [file_name] user_name@user_host:<folder>

Example:

scp -v apache-tomcat-9.0.8.tar.gz [email protected]:team

Output:

gaurav@ubuntu:~$ scp -v apache-tomcat-9.0.8.tar.gz [email protected]:team
Executing: program /usr/bin/ssh host 159.89.170.11, user root, command scp -v -t team
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /home/gaurav/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 159.89.170.11 [159.89.170.11] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /home/gaurav/.ssh/id_rsa type -1
apache-tomcat-9.0.8.tar.gz                                                                                          100% 9589KB  99.8KB/s   01:36    
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 9826736, received 4016 bytes, in 97.2 seconds
Bytes per second: sent 101133.9, received 41.3
debug1: Exit status 0
gaurav@ubuntu:~$

Transferring files between two remote hosts wirelessly.

You can transfer files and directories between two remotes hosts.

General Syntax:

scp remote_user_1@host_1:/[file_name] remote_user_2@host_2:[folder_to_save]

The syntax seems a bit wider but is pretty simple.

Example:

scp -r [email protected]:gaurav [email protected]:/team

Output:

gaurav@ubuntu:~$ scp -r [email protected]:/gaurav [email protected]:/team 
[email protected]'s password: 
1.py                                          100%  134   261.3KB/s   00:00    
variables.py                                  100%  377   949.2KB/s   00:00    
abc.txt                                       100%    0     0.0KB/s   00:00    
ath.html                                      100%  199KB  41.8MB/s   00:00    
gaurav@ubuntu:~$

Transfer files from the remote system to the local system.

You can easily transfer the files or directories from the remote system to your local system .

General Syntax:

scp remote_username@user_host:/files/file.txt /[folder_of_local_system]

Output:

gaurav@ubuntu:~$ scp [email protected]:how.txt .
[email protected]'s password: 
how.txt                                                                                                             100%   11     0.1KB/s   00:00    
gaurav@ubuntu:~$

Output:

gaurav@ubuntu:~$ ls -l how.txt
-rw-r--r-- 1 gaurav gaurav 11 Oct  6 09:49 how.txt
gaurav@ubuntu:~$ 

Compressing files to make quicker transfers.

Using the -Coption compress the larger size files facilitates a faster transfer.

General Syntax:

scp -C [file_name] user_name@user_host:[target_folder]

Transfer without -C option:

gaurav@ubuntu:~$ scp -rv dlink [email protected]:team
Executing: program /usr/bin/ssh host 68.183.82.183, user root, command scp -v -r -t team
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /home/trinity/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 68.183.82.183 [68.183.82.183] port 22.
debug1: Connection established.

debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 7516504, received 4008 bytes, in 74.6 seconds
Bytes per second: sent 100693.7, received 53.7
debug1: Exit status 0
gaurav@ubuntu:~$ 

Transfer with -C option:

gaurav@ubuntu:~$ scp -Crv dlink [email protected]:team
Executing: program /usr/bin/ssh host 68.183.82.183, user root, command scp -v -r -t team
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /home/trinity/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 68.183.82.183 [68.183.82.183] port 22.
debug1: Connection established.
.
.
webupload.img                                                                                                       100% 1834KB  98.7KB/s   00:18    
Sending file modes: C0664 1877552 router.img
Sink: C0664 1877552 router.img
router.img                                                                                                          100% 1834KB 100.3KB/s   00:18    
Sink: E
Sending file modes: C0664 3754103 DSL-2750U-Release-IN-T-01.00.07.zip
Sink: C0664 3754103 DSL-2750U-Release-IN-T-01.00.07.zip
DSL-2750U-Release-IN-T-01.00.07.zip                                                                                 100% 3666KB 218.5KB/s   00:16    
Sink: E
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 7518864, received 3828 bytes, in 51.0 seconds
Bytes per second: sent 100245.4, received 51.0
debug1: Exit status 0
debug1: compress outgoing: raw data 7511925, compressed 7513136, factor 1.00
debug1: compress incoming: raw data 1467, compressed 999, factor 0.68
gaurav@ubuntu:~$

Here, we can easily observe that using the -C option with the scp command has allowed us to compress the file over the network thus proving to be a time-saver option.

Using a different SSH port for file transfer .

While using the scp command the default port deployed is port 22.Scp command to use the port of your choice.

General Syntax:

cp -P [new_port_number] [file_name/directory_name] remote_user@host:[destination_folder]

Example:

scp -P 4248 dlink [email protected]:team