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:
- How Does rsync Command Work to Transfer Files?
- Transfer File From Local to Remote
- Transfer File From Remote to Local
- Synchronize the Files on Local and Remote Servers
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.