This video will teach you how to copy files to a remote server using the command line in Linux. This lesson will be useful to you when you want to copy a file or a folder from your local computer to a remote server or PC. Not only from local to remote but also you can copy files or directories between the servers that are in remote locations, that is, one remote host to another remote host. So, let’s get started.
Let’s assume I have a folder or directory on my computer and I want to send it to a server in a remote location. I can achieve it if I have an SSH access to the server. Let’s check if I can login to the remote server using SSH. To login to the server via ssh, type the username@IP or hostname of the remote server and hit enter. Type the password and hit enter. If the credentials are correct, you will be able to get access to the remote server’s console.
Checking the Linux distribution with cat /etc/os-release, we can see it’s a Ubuntu 20.04.5 LTS server.
Before copying files/directories to the server, I want to create a directory here on which the copied file will be stored. Clearing the screen and long listing the contents of the root directory, we can see the new directory is created. Let’s get into the new directory using cd and the directory name and typing pwd and hitting enter, we can see the location of the directory printed on the screen. We will send the files to this location of the remote server from our computer.
My friends, up until now, we have just grabbed the background of the lesson. Now, let’s move on and copy/send the files. For this I open a new terminal, for your information, this is a Windows computer but I can run Linux commands using the GitBash software. And this is my local computer’s terminal. I change the current working directory to Downloads using the cd command and listing the contents, we can see some files and directories. Out of them, I am going to copy the SiteFiles directory to the remote server.
To copy or send a file or directories, we can use the scp command in Linux. S-C-P stands for a secured copy. So, let’s copy the folder/directory to a remote server using the SCP. For this, scp -r, the local file/directory location and name, space username@IP or hostname of the server, colon the remote server directory path where the file will be stored. Here, you should use -r option if you are copying a folder/directory but if you are sending or copying a single file, the -r option is not required.
After you hit enter, you will be prompted with the password, type the password and hit enter. Copying the file/folder from the local computer to the remote server begins now. We can see, the files on the directory are being copied to the remote location recursively.
After the copy is complete, to verify the file reached there, go to the remote server’s location and run ls -al command. You will see the directory copied there.
Okay, with that we have learned how to copy a directory to a remote server’s directory. Now, let’s move on and learn how to copy a file such as an archived file to a remote server.
On the local computer’s terminal, let’s create an archive of the directory named SiteFiles using the tar command with -cvzf options, the archive file name, and the directory name. The archive is created now. Again, listing the content, we can see the archive named site1.tar.gz is created.
Finally, let’s copy the file to the remote server using the scp command. Type scp, we don’t need -r option because we are copying a single file, the file name, root@ip or the hostname of the server, colon, the remote server’s destination directory path, and hit enter. Enter the password of the remote server’s user and hit enter. If the credentials are correct, the file will be copied to the remote server securely.
To verify, login to the remote server, go to the destination directory path and run ls -al. We can see the file is copied there.
In the same way, you can copy any type of file or directory from one server to another or from one PC to another PC using the terminal. In the next video, we will learn how to copy a file or a directory from a remote server to the local computer or from the destination host to a source host.