• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Use SCP Command to Transfer Files and Folders

Written by Admin, Updated On May 11, 2020
scp, terminal
How to Use SCP Command to Transfer Files and Folders

SCP (secure copy) command line tool in Linux that is used to copy files over the networks in a secure way. It can be used to copy files between the local host and the remote host or between two remote hosts. It uses same authentication mechanism as used in the Secure Shell (SSH) protocol.

In this guide, we will see how to use the scp command with practical examples and common scp options.

SCP Command Syntax#

Below is the basic syntax for SCP command:

scp [OPTION] [[username@]src_host:]file1 [[username@]dest_host:]file2

Here,

  • OPTION – scp options such as cipher, ssh configuration, ssh port, limit, recursive copy, etc.
  • [[username@]src_host:]file1 – It’s source file which need to copy.
  • [[username@]dest_host:]file2 – Destination where to copy.

For local files provide absolute path and relative path for remote host including user and host details.

Some of the most widely used options in scp command are listed below:

  • -P ssh port number of remote host
  • -p Preserves permissions and access time of files
  • -r Copy files and directories recursively
  • -C Enable Compression
  • -i identity File or private key
  • -l limit the bandwidth while copying
  • -q Suppress warning message of SSH
  • -v verbose output

Make sure you have at least read permissions on the source file and write permission on the target system to be able to copy files.

Copy Files and Directories using scp#

Following are the examples to copy files from local to remote, remote to local and between two remote systems.

Copy a Local File to a Remote System with the scp Command#

Run below command to copy a file from local to remote system:

scp info.php tecnstuff@22.33.44.55:/var/www/html

Here, info.php is the file which we want to copy and tecnstuff is the user of remote system. 22.33.44.55 is the ip address of remote system. The /var/www/html is the path to the directory where you want to copy the file.

It will ask you to enter password and the copy process will start.

tecnstuff@22.33.44.55's password:
info.php 100% 0 0.0KB/s 00:00

Give new file name with remote path, if you want to set other than the original:

scp info.php tecnstuff@22.33.44.55:/var/www/html/index.php

SCP command to copy directories is same as file just you have to specify the -r option for recursive.

scp -r local/directory tecnstuff@22.33.44.55:/remote/directory

If you SSH port of remote host is different than default 22 then you can specify using -P flag as below:

scp -P 3452 info.php tecnstuff@22.33.44.55:/var/www/html

Copy a Remote File to a Local System using the scp command#

To copy file from remote host to local system, set remote file location as source and local path as the destination.

For example to copy a info.php from a remote server with IP 22.33.44.55 run the following command:

scp tecnstuff@22.33.44.55:/var/www/html/info.php /local/directory/info.php

Copy a File Between Two Remote Systems using the scp Command#

It doesn’t required to login to either system to copy file between two remote systems:

For example, Run the below command to copy info.php file from host 22.44.55.66 to 55.44.22.11 :

scp host1user@22.44.55.66:/home/info.php host2user@55.44.33.11:/home

It will copy info.php file from /home directory of 22.44.55.66 to another host 55.44.22.11.

You will be asked to enter password for both remote accounts. On success file will be transfer from one to another system.

Conclusion#

In this tutorial, you learned how to use the scp command to copy files and directories.

If you have any question or feedback, leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install Python 3.8 on Debian 10
Next Article   How to Set Up a Firewall with UFW on Ubuntu 20.04

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Jenkins on Debian 11
    How to Install Jenkins on Debian 11 January 5, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook