Scp command

The scp command is a command-line utility primarily used to securely copy directories or files between two hosts or locations.

Before we dive into scp commands and how to use them, we’ll go over the basic syntax behind the scp command and a few pointers to take note of.

Pointers to Take Note of Before Using SCP Command

Using the scp command allows you to transfer or copy files in three ways:

  1. Via a remote system to a local system
  2. Via a local system to a remote system
  3. Between two remote systems from a local system

Key points to remember when using the scp command:

  1. Scp uses SSH when transferring or copying data
  2. You will need an SSH key or password to allow the transfer of files and validate the remote systems
  3. The user who wants to transfer a file using the scp command should read the permission of the source file
  4. For remote files, you must specify the user and host specifications
  5. If you want to copy any file that has the same name and location, the scp command will automatically overwrite the files without providing any warning
  6. Make sure you write permission consent on the target system (remote or local) when copying files
  7. It is encouraged to run a tmux session if you want to transfer large files

SCP Command Syntax

The basic command syntax will take the following form:

scp [OPTION] [user@]SRC_HOST: ]file1 [user@]DEST_HOST: ]file2

where,

OPTION – Refers to the scp options such as SSH configuration SSH port, cipher, recursive copy, etc.

 [user@]SRC_HOST: ]file1 – Refers to the source file

[user@]DEST_HOST: ]file2 – Refers to the destination file

Using scp command will also use common options or parameters to allow a user to control a feature easily such as:

scp -p – preserves files from any modification or from being accessed

scp -P – Defines or specifies the SSH port of the remote host

scp -C – Enables compression of any data to improve file transfer speed

scp -r – Copies files recursively

scp -q – Allows you to suppress non-error messages and the progress meter

(:) – Differentiates between a remote and local destination

scp -v – Refers to the verbose output

scp -i – Allows you to identify a file or private key

scp -l – Allows you to limit the bandwidth when copying or transferring files

Now that we’ve laid down the basic syntax and pointers when using the scp command, we can dive into the examples and how to use it in the command line.

How to Copy a Local File to a Remote System

To copy a file from a local system to a remote system, type the command:

scp sample.txt remote_dave@10.10.0.5:/remote/directory 1

where,

sample.txt – Refers to the file you want to copy or transfer

remote_dave – Refers to the remote_username or user of the remote server

/remote/directory 1 – Refers to the directory path you want to copy files to

10.10.0.5 – Refers to the user IP address

Once you’ve set all these parameters, the command scp will copy files to the directory specified; however, if you don’t specify a path, it will automatically be copied to your home directory.

Below is the output once you’ve entered the user password:

remote_dave@10.10.0.2's password: sample.txt 100% 0 0.0KB/s 00:00

Another example would be if you want to change the file name or save the file under a new name, you have to specify this in the new command:

scp sample.txt remote_dave@10.10.0.5:/remote/directory 1/newsample.txt

In case the SSH on the remote host is listening to a different  SSH port apart from the default SSH port (port 22), then you can specify a new SSH port using the scp -P option as shown below in the example:

scp -P 2322 sample.txt remote_dave@10.10.0.5:/remote/directory 1

You’ll also find there are various SSH ports you can use apart from SSH port 2322. 2022, for instance, is another port you can use in this example or scenario.

Now, what happens if you want to copy a directory like local directory 1 using scp?

The command is similar to when you copy a file, only that we’ll use the scp -r option to copy a file recursively as shown below in the example:

scp -r local directory 1 remote_dave@10.10.0.5:/remote/directory 1

How to Copy a Remote File to a Local System

Copying a remote file to a local system is just as straightforward as copying a local file to a remote system.

The only difference is that the remote location becomes the source while the local system becomes the destination or receiver.

For example, if we want to copy a remote file named sample.txt, type the command:

scp remote_dave@10.10.0.5:/remote/sample.txt /local/directory 1

Once you’ve entered the password on the remote system, enter the password to allow the transfer process to follow through.

How to Copy Files Between Two Remote Systems

Using the scp command between two remote systems will not require to login to the server to transfer or copy files.

For example, let’s say you want to transfer a file named /files/file.txt between two remote hosts: hostA.com and hostB.com along with the directory /files.

To copy “/files/file.txt” run the command:

scp user1@hostA.com:/files/file.txt user2@hostB.com:/files

where,

/files/file.txt – Refers to the files to copy or transfer

hostA.com – Refers to the remote system

hostB.com – Refers to the other remote machine

/files – Refers to the directory you want to transfer the files to

Another example would be if you want to direct traffic via the machine that the command was issued, where in this case, we use the -3 option:

scp -3 user1@hostA.com:/files/file.txt user2@hostB.com:/files

How to use the -C Parameter

Using the scp command with the -C parameter is useful for compressing the file and speed up the transfer process. With the -C parameter, you’ll be able to compress the file on-the-go.

To use the scp command with the C-parameter, run the command:

dave@mint ~/My_files $ scp -Cpv sample.txt barnes@202.x.x.x:.

where,

dave@mint – Refers to the username

/My_files – Refers to the destination folder you want to copy or transfer the file to

sample.txt – Refers to the file you are transferring or copying

barnes@202.x.x.x – Refers to the destination host

This scp command with the -C parameter will enable compression in the source and decompression of the file in the destination host.

How to Copy Large Files

Using scp to transfer large files as mentioned earlier in the pointers, requires using a tmux session. To make sure you have tmux installed, run the command on your terminal:

tmux -V  

In case you don’t have tmux installed, simply open your terminal and type the command (for Ubunutu & Debian based systems):

sudo apt-get install tmux

For CentOS and RedHat:

sudo yum install tmux

Final Thoughts

Using the scp command ensures authenticity and confidentiality when transferring and copying data from a remote host to your local host and vice versa.

You’ll find that using several parameters such as scp -C, scp -v, scp -l, etc. can help establish an scp secure connection and even provide better functionality when using the scp command.

As a last minute-tip, we recommend using an SSH config file if you connect to the same systems regularly. In effect, this will allow you to simplify your workflow process. 

We hope this tutorial was able to guide you effectively on how to use the various commands for scp!

We’re excited to hear your feedback, so please don’t hesitate to let us know how you felt towards this tutorial in the comments section below!