How to Transfer Files With Rsync Over SSH?

The rsync (Remote Sync) is the command utility that transfers files and directories over SSH.

It is useful to synchronize files between remote systems. Administrators have mostly utilized the rsync command for backup data, mirroring, and modifying files or directories over the network.

This post will address the method to transfer files with rsync over SSH. The content that is followed the article is mentioned below:

Let’s start with the basic working of the rsync command.

How Does rsync Command Work to Transfer Files?

The “rsync” command transfers single or multiple files between remote and server systems. For this, we considered multiple examples to exchange the files between them. To transfer a file, follow the below syntax:

Local System to a Remote Server:

$ rsync local_file_path user@remote-host:remote_file_path

Remote Server to the Local System:

rsync user@remote-host:remote_file_path local_file_path

In the above syntax, the argument is explained as below:

  • local_file_path: It specifies the file that the user wants to transfer to the remote server.
  • user@remote-host:: It represents the username and hostname of the remote server.
  • remote_file_path: refers to the file or directory path that the file will transfer.

Example 1: Transfer a File From a Local System to a Remote Server

An example is considered to transfer a file “script.sh” from the local system to the “remote” server via the rsync command. This command requires the username, hostname, and destination directory defined below:

$ rsync script.sh roger@ubuntu:Downloads

The executing command requires the authentication of the remote server.

Verify the Transferred File

To verify the transferred “script.sh” file, access the destination directory “Downloads” via the “cd” command as below:

$ cd Downloads

The output returns the “script.sh” file in the “Downloads” directory.

Let’s explore another example.

Example 2: Transfer a File From a Remote Server to a Local System

For transferring the “file.txt” from the remote server to the local system via the “rsync” command. For instance, the below script will transfer the file into the “Folder” directory in the local system:

$ rsync roger@ubuntu:file.txt Folder

The above command requires the authentication of the remote server.

Verify the Transferred File

To verify the transferred “file.txt” file, access the destination directory “Folder” via the “cd” command as below:

$ cd Folder

The output confirms the presence of “file.txt” in the “Folder” directory.

Example 3: Synchronize the Files on Local and Remote Servers

To synchronize the files on a local and remote server, you can use the “rsync” command with the “–delete” flag. The -a flag tells rsync to preserve the file attributes, and the -v flag enables verbose output. The -z flag enables compression to speed up the transfer.

Here is an example of how you can use rsync to synchronize the files in a local directory with a remote server:

$ rsync -avz --delete Downloads roger@ubuntu:Folder

This command will synchronize the files in the “Downloads” directory on the local machine with the “Folder” directory on the remote server. The “–delete” flag tells rsync to delete any files in the destination directory which not present in the source directory.

That’s how transferring files is possible through rsync over SSH.

Conclusion

In Linux, the “rsync” command transfers files between remote and server systems over SSH. Using this command, users can exchange single or multiple files by specifying the <username> and <hostname> of the remote systems. This guide has explained possible examples of transferring files with rsync over SSH.