SCP Directory transfer in Linux: How to do it

scp

scp directory

When you are working with multiple Linux and other devices running on Unix based operating systems, you may face situations where you will have to transfer files between local and remote machines. To do this securely, you can use secure copy protocol i.e., SCP. SCP is a command line utility that helps you transfer files and directories over an encrypted SSH connection. The security and easy-to-use nature of SCP makes it the choice of developers and system administrators to transfer files and directories between connected machines. In this article, let us learn how to effectively do SCP directory transfers, and as an additional bonus, let us take you through all the features available. 

What is SCP?

If you are a regular patron of Unixmen, you’d have almost expected this section. At Unixmen, we always start with the basics i.e., functionality of each command or utility so that you can wield them and tweak them as per your requirements. SCP stands for Secure Copy Protocol. It is secure since it uses SSH (secure Shell) to transfer files between a local and remote machine or between two remote machines. Your usual command in this use case might be “cp”. SCP directory transfer combines the practicality of standard file transfer utility cp, but with the security of SSH encryption.

Your next question would be: why not rsync? While rsync provides advanced features (more on this in another article), SCP is still the favorite for developers and system administrators because it is quick, easy to use, and comes out-of-the-box on most Linux distros (distributions).

How to Use SCP?

The basic syntax for SCP is:

scp [option] [source] [destination]
  • Option: We have explained the options available in this article
  • Source: The path to the file or directory you intend to transfer
  • Destination: The path where the file or directory will be copied to. The destination path can be either local or remote.

But when you are working with remote servers, specify the remote path in this format:

username@remote_host:DestinationPath

How to Transfer Directory Using SCP?

Let us see some examples of SCP directory transfers. The important thing to note is the “-r” option (recursive) is required for this option. This lets SCP to copy files and contents of directories.

How can I copy a Local Directory to a Remote Server?

To copy an entire directory from your local machine to a remote server, execute the following command:

scp -r LocalDirectory username@remote_host:RemoteDirectory

Let us learn about the command params better:

  • r: Instruct SCP to recursively copy all files and subdirectories within the specified directory
  • Local directory: The path to the directory on the local machine
  • Remote directory: The destination path on the remote server along with the credentials for authentication

How to Copy a Remote Directory to a Local Machine

We just learnt how to copy a directory from a local machine to a remote machine. In cases where you would want to do the reverse i.e., copy a directory from remote server to local machine, simply reverse the order of the argument.

scp -r username@remote_host:RemoteDirectory LocalDirectory

If you notice closely, we simply reversed the order of arguments. Notice the credential patterns carefully when you use this command.

Options Available for SCP Directory Transfers

We mentioned at the start of this article that there are options available in the SCP directory transfers and here is where you can learn all that. You can combine a variety of options along with the recursive directory to optimize performance and security.

To Compress

SCP allows compression on the go with the -C option. When you want to transfer directories or files that are too large, you can compress it to speed up the process and also to reduce the amount of data transmitted over the network. Here is how you can do this:

scp -r -C LocalDirectory username@remote_host:RemoteDirectory

To Specify Port Number

If your remote server is using a non-standard SSH port, you have to specify the port with the -P option. 

scp -r -P 1234 LocalDirectory username@remote_host:RemoteDirectory

Executing this command tells SCP to connect to the remote host on port 1234 instead of the port SSH uses by default i.e., 22.

To Provide SSH Key

If you are using SSH key authentication instead of password, you can specify the key file using the -i option. 

scp -r -i PrivateKeyPath LocalDirectory username@remote_host:RemoteDirectory

To Enable Verbose Mode

To get more detailed output (in other words, enable verbose mode) during the SCP operation, use the -v option.

scp -rv LocalDirectory username@remote_host:RemoteDirectory

Alternative for SCP Directory Transfer

We mentioned rsync as an alternative for SCP for directory transfers. Let us see the features rsync offers to see if you should choose rsync or SCP. Here are some features that set rsync apart:

  • Resuming incomplete transfers
  • Synchronizing directories efficiently i.e., transfers only changes
  • Better handling of large file structures

The syntax of rsync is similar just that the output is more powerful.

rsync -avz LocalDirectory username@remote_host:RemoteDirectory

The -a option ensures archive mode to preserve permissions and timestamps. The -v option enables verbose output to make debugging easier. The -z option enables compression.

Best Practices for SCP Directory Transfer

SCP uses SSH for its communication. This makes it generally secure but still there are a few best practices to follow.

  • Always use the -i option to enable key based authentication instead of password-dependent logins for better security.
  • Ensure the remote server’s identity via the SSH fingerprint when you connect to the remote server to identify any man-in-the-middle attacks.

Wrapping Up

SCP directory transfer is simple and straight-forward. Whether you are managing backups, deploying code, or simply transferring files between servers, SCP is a reliable option. SCP, if used effectively, can help you simplify workflow and enhance security.

Related Link

Ubuntu Manpage: scp — secure copy (remote file copy program)