The scp command is used for securely copying files between remote machines. Its syntax is similar to the widely used cp command and thus it is preferred for quickly copying a few files from the remote system to local one.
To copy files from the remote system to the current working directory in local system, use the scp command like this:
scp remote_username@remote_IP_address:file_location .
The important part is
- Here, remote_username is the username on the remote server. You need to know its password, of course.
- Then, you combine it with the IP address of the remote server with the help of @.
- The colon
:is important and it is followed by the location of the file or directory on the remote server.
.is for the current working directory of the local system from where you are running the scp command. You can switch to the desired directory while running it or use its path.
If it all seems a little confusing, let me demonstrate it with a practical example.
Copying files from remote to local using scp command
Here's the demo scenario. I deployed an Ubuntu server on Linode. Its IP address is 188.8.131.52.
And there I downloaded some files in the remote system, in the location
/home/abhi/dev/text-files/. Actually, I cloned this GitHub repository.
Here's what it looks like:
Now that you are familiar with my test setup, let's use scp to copy files to the local system.
Copy a single file to the local system with scp
Let's say I want to copy
agatha.txt file to my local system. Here's what I will use:
scp [email protected]:/home/abhi/dev/text-files/agatha.txt .
As I mentioned earlier, you need to know the password of the remote user.
Here's what the entire procedure looks like:
Copying a directory from remote to local using scp
Copy file was nice but what about copying an entire folder? Like copying directories with the cp command, you can use the recursive option
-r with scp, too.
Let's say I want to copy the
i3_config_files folder from the remote system. I'll use:
scp -r [email protected]:/home/abhi/dev/text-files/i3_config_files .
And here's what it looks like:
-pand keep the same timestamps as the original file. The file ownership will still be changed, though.
The other way around?
If you want to copy multiple files to the local, I advise logging into the remote system and from there, sending the files to the other system. That's because you have to type the entire file path and with remote user and IP, it will be too much trouble.
scp file1 file2 user_name@IP:path_to_dir
But that also creates a problem for home users. Your server may have a public IP but your personal computer is sitting behind a router (NAT) and uses private IP. It won't be accessible from outside the world unless you employ advanced tactics like tunneling.
As a simpler workaround, ssh into the remote server and put those multiple files you intend to copy in a directory. Now, you can copy this directory from your local machine.
I hope you find this quick little tip helpful in copying files from the remote system to local. Let me know if you have questions.