How to Securely Copy Files using SCP Examples in Linux Terminal


Get the best SCP Commands examples to Securely transfer Files and Directories from Remote Host to Localhost and Localhost to Remote or between Two Remote Hosts Server in this Linux Guide. SCP command is used to copy the file from one place to another place securely in Command Line Tool that is in Terminal. It uses the same SSH (Secure Shell) authentication and Protocols when connecting between two hosts. So, you can transfer the files securely with SCP in Linux.

The same SCP command can be used for Apple Mac OS too.

Note that, unlike GUI copying, if there is same file exist in destination, SCP command will simply overwrite the file in destination. Be careful when copying the file source to destination with same name. In that situation, just rename anyone of the file in the hosts.

SCP Sample Example:

scp -r  root@192.122.112.121:/var/www/public_html/ /home/st/

Easy SCP Examples to Transfer Files / Folders(DIR)

Basic Syntax

First, lets see the basic syntax of the SCP command,

scp source_file_name username@destination_host:destination_folder

source_file_name is the file to copy.

username@destination_host is destination location. For example: root@192.168.0.2  or root@example.com.   That is root User Account of that Destination Host(IP or Domain Name)

destination_folder the folder you want transfer the file into.  That is public_html folder in the root User Account.

Remote Host to Local Host

If you want to copy file from Remote Host to Localhost use the following SCP Example.

raja@st:~#   scp username@source_host:sample_file.txt /local_folder/subfolder/

If you want to Copy the File from Local Host to Remote Host, Use the Following SCP Example.

raja@st:~#   scp sample_file.txt username@destination_host:/remote_folder/subfolder/

SCP Remote to Remote Host

Use the following SCP example to transfer the files from Remote Host to Remote Host. For example. Use this two copy files from one server to another server remotely.

raja@st:~#   scp username@source_host:/source_folder/subfolder/sample_file.txt username@destination_host:/remote_folder/subfolder/

Let’s see the Advanced usage of SCP with parameters,

scp man will display manual of the scp command in terminal

-P port
Specifies the port to connect to on the remote host. Note that this option is written with a capital ‘P’, because -p is already reserved for preserving the times and modes of the file in rcp.

-p‘ Preserves modification times, access times, and modes from the original file.

-q‘ Quiet mode: disables the progress meter as well as warning and diagnostic messages from ssh.

-r‘ Recursively copy entire directories. Note that scp follows symbolic links encountered in the tree traversal.

-S program
Name of program to use for the encrypted connection. The program must understand options.

-v‘ Verbose mode. Causes scp and to print debugging messages about their progress. This is helpful in debugging connection, authentication, and configuration problems.



SpicyTricks
© 2024 SpicyTricks.com . All rights reserved.
Follow Us On