Blog Post
Examples on How to Use the scp Command in Linux
Commands

Examples on How to Use the scp Command in Linux

The command line in Linux is an incredibly powerful utility. You can do a wide variety of things by simply typing lines of text. In this guide, I will focus on using the scp command through examples so you can understand it easier. This will allow you to copy directories and files from one location to another securely.

Here is how the secure copy command will help you:

  • Copy files from your local system to a remote system (server) and vice versa
  • Use your local system to copy files between two remote systems

The crucial thing to remember is that the scp command keeps your files encrypted so that no one can take your personal or otherwise sensitive information.

The great thing about the secure copy function is that it is not difficult to use it. Take a look at the guide below and see if I am right.

The Basic Syntax

First, if you don’t have a Linux distro installed I have a guide on how you can install Ubuntu on your machine. Now let’s take a look at how the basic syntax of the scp command looks. Take a look at the following:

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

Now, here is a detailed explanation of the meaning of what is written above:

  • Option – here you will give additional instructions, such as choosing the desired ssh port and configuration, limit, etc.
  • File 1 – the source file that you plan to copy
  • File 2 – the preferred destination file

If you plan to copy a local file, you can use both relative and absolute paths to specify its location. On the other hand, do not forget that remote systems require you to specify the user and the host.

Here are some of the options that you can use to control the scp command:

  • -p – keeps access and file modification times.
  • -P – used to specify an ssh port of the remote host.
  • -r – order the command to perform recursive copying.
  • -c – the system will try to compress the files as it copies them to the destination system.
  • -q – suppress the non-error messages and the progress meter.

What Else You Should Know about scp command

  1. What makes the scp command secure is that it utilized SSH when transferring data. If you are using a remote system, you will be required to provide a password or an ssh key
  2. When using a “:” (colon), you allow the system to make a difference between a remote and local path.
  3. The secure copy command won’t warn you before an overwrite! It will automatically overwrite the files of the same name, which is why you need to be extra careful if you are copying destinations or files that have the same names on both systems.
  4. Use a “tmux” or “screen” session to utilize the scp command if you are moving big files (find more about tmux or screen).

How to Use the scp Command Copy a File from a Local to Remote System

If you are copying from a local system and remote system is the desired destination, here is how your command should look:

scp file1 remote_user@11.11.1.1:/remote/folder

Change “file 1” to the name of the file you want to copy and don’t forget the extension. In the “username” part enter the name of the remote system user, and change the numbers to the corresponding IP address of the system.

Finally, “remote/folder” marks the path to the destination where you want to copy the file. Unless you specify a particular folder, the scp command will copy the file to the root directory of the remote user.

If you want to rename the file you are copying; you can do so by adding the desired filename at the end of the destination path – “remote/folder/filename.txt.”

As I mentioned before, you will need to enter the password to authenticate the remote user.

The system will confirm that the copying process was completed.

The command mentioned above will use the default port 22 on the remote host. However, if your SSH is listening to a different port, you will need to specify it.

In that case, the command will look like this:

scp -P 23 file1 remote_user@11.11.1.1:/remote/folder

Change the number “23” to your preferred port and keep in mind that you need to use the uppercase letter for issuing the right instruction.

How to Use scp Command to Copy a Folder from a Remote to Local System

The command looks almost identical to the one you use for copying files. Here is the form:

scp -r /local/path/ remote_user@11.11.1.1:/remote/path

You want to specify the user on the remote system, as well as the IP address. Aside from that, it is vital to use the “-r” option so that the system executes recursive copying.

How to Use scp Command to Copy Files and Folders from a Remote to a Local System

In essence, this scp command will be a reversed version of the ones above. If you want to copy a file, it will look like this:

scp remote_user@11.11.1.1:/remote/file.txt /local/path

On the other hand, copying a folder looks like this:

scp remote_user@11.11.1.1:/remote/path /local/path

All the other rules apply, too, which means that you will need to provide a password to authenticate the remote user.

How to Use scp Command to Copy Files Between Two Remote Systems

The great thing about the scp command is that you do not have to log in to either of the servers to copy files between two remote systems. The only thing you will need is to provide passwords to authenticate both accounts.

Here is how the command will look like:

scp user1@host1.com:/directory/filename.txt user2@host2.com:/directory/directory

In the situation above, I will copy the “directory/filename.txt” from the first remote server to the “directory/directory” destination on the second remote system. The file will be copied under the same name. You can rename it by using the “directory/directory/desired_file_name.txt” path.

The interesting thing to know is that you can use the “-3” option to route the transfer through the computer where you are issuing the command.

scp -3 user1@host1.com:/directory/filename.txt user2@host2.com:/directory/directory

Wrap Up

Did you enjoy our detailed guide on using the scp command? It is an incredibly useful utility that allows you to copy files and folders securely and effortlessly.

If you want to make things even easier, you can adjust a key-based SSH authentication, which will allow you to connect to remote servers without providing a password every time.

Alternatively, you can also manage your connections in the “SSH config,” but you should only do so if you are always connected to the same systems.

Related posts

Leave a Reply

Required fields are marked *

Copyright © 2022 Blackdown.org. All rights reserved.