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
- 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
- When using a “:” (colon), you allow the system to make a difference between a remote and local path.
- 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.
- 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 firstname.lastname@example.org:/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 email@example.com:/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/ firstname.lastname@example.org:/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 email@example.com:/remote/file.txt /local/path
On the other hand, copying a folder looks like this:
scp firstname.lastname@example.org:/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 email@example.com:/directory/filename.txt firstname.lastname@example.org:/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 email@example.com:/directory/filename.txt firstname.lastname@example.org:/directory/directory
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.
If you are a fan of using Python programming language, you can make your life easier by using Python Pip. It is a package management utility that allows you to install and manage Python software packages easily. Ubuntu doesn’t come with pre-installed Pip, but here…
If you are looking for a convenient way to manage Java applications on Ubuntu, the solution may lie in the IntelliJ IDEA development platform. An excellent advantage of this option is that it is relatively suitable for beginners, but experienced users will also find it…