Register Login

Copying a Directory with SCP

Updated Sep 02, 2021

Unix is one of the most powerful operating systems. In Unix-based operating systems, we can use the SCP (Secure Copy Protocol) (using the SCP command) for securely copying files and directories from one host to another remotely without commencing an FTP session or even explicitly logging into any remote system.

In this tutorial, you will learn about simple operations that will implement Secure Copy Protocol for transferring and managing files.

What is secure copy protocol (SCP)?

The secure copy protocol (SCP) is a protocol that supports securely uploading or downloading data or files to and from any remote system. The scp command helps in utilizing the SSH for transferring data. During this process, it requires a password or passphrase that helps in authenticating the process.

SCP also encrypts the file and the passwords exchanged for authentication because any malicious person if snooped into the network won't be able to read or decrypt the files or data. The SCP command runs on port number 22.

It uses the rate control protocol (RCP) for transferring the files and the SSH protocol for rendering authentication & encryption. So, SCP is a blend of two protocols.

Syntax:

scp [options] user_name1@source_host:directory1 / file_name1 user_name2@destination_host:directory2 / file_name2

where, the username1@source_host : directory1/file_name1 specify the source file location, which includes the following:

  • It is the account name on the host computer (user_name1)
  • The hostname defines the computer name on which your source file is residing (source_host)
  • The directory name defines the directory location that is holding the source file (directory1)
  • This (file_name1) defines the filename of the source file

Creating Directory by Downloading files or directory using SCP:

If you want to use the secure copy protocol for downloading any file from the remote server to your local machine, you have to use the command in the terminal along with the -r flag. It will tell the scp command to recursively copy all of the directory contents to your local machine.

$scp -r userName@ssh.example.com:/path/to/remote/sourceLoc /path/to/local/destinationLoc

This is too simple to use, isn't it? The only difference between downloading any single file and downloading the complete directory is the -r flag used with the scp command.

This -r flag is used as the directory tree that can recursively travel each file within that directory and download those it encounter.
If the source directory does not exist in the mentioned target location on host, the command will create a directory. If, however, the target path does not exist, you will encounter an error or issues and this might result to the intended scp command to fail.

Uploading a directory using SCP:

The uploading technique also uses the same mechanism as that of downloading. The only difference that you might probably notice is the source directory within the actual command.

Yes, we have to specify the source directory with a specific actual command.

Let us now take an example of a code that uses the scp command to upload a folder:

$scp -r /path/to/local/sourceLoc userName@ssh.domainPath.com:/path/to/remote/destinationLoc

Here you have seen that the source (sourceLoc) path comes first. This assumes that it is referring to your local machine's directory. It is then recursively assigned to the destination machine using the -r flag, the way you did before.

Conclusion:

If you want to know more about the scp command, you can type the command:

“man scp”

in your terminal to check out the documents on each of its commands and syntaxes. This command is one of the simplest and secure (encrypted) way of transferring data remotely across machines. Apart from creating a directory, uploading and downloading files through it, users can also use SCP within the Putty () tool to create a secure communication, transfer files remotely, etc.

 


×