How to rsync a directory?

Rsync stands for “Remote Sync.” It is a synchronization method for directories and files that works both locally and remotely. This tool employs the “delta algorithm” that reduces the amount of copied data by only transferring the modified part of the files or directories. In this post, we will go through the basics of using this tool to sync a directory.

What is Rsync?

Rsync is a network-enabled utility that is highly flexible in terms of syncing files and directories. It is included in most of the Unix and Linux distributions as a popular utility for system scripts and its widespread use on these operating systems.

Now, let’s check out the basic syntax of the rsync command.

Rsync Syntax:

$ rsync Option Source Destination

Here “Option” refers to rsync options, “Source” for the source directory, and “Destination” for the destination directory.

To demonstrate to you the method of syncing a directory in Linux, we created two test directories, “testdir1” and “testdir2,” in our system.

1. Rsync directory locally:

First of all, list out all the directory content to get to know the files or folders. We have selected “testdir1” for this purpose.

Rsync directory with the “-r” option:

For direct syncing, utilize the recursive “-r” option in the rsync command. The below-given syntax will sync the content from the source to its destination directory. The “/” points towards the content of the source directory. Without this slash, the rsync command will place the source directory within the destination directory.

Write out this command to start local syncing a directory to another.

$ rsync -r testdir1/ testdir2

$ ls -al ./testdir2/

List out the “testdir2” content to view the changes made by rsync.

Rsync directory with the “-a” option:

You can also utilize various options or flags that rsync supports. “-a” flag is one of them. This flag helps the rsync command sync a directory recursively and maintain the device files, modification times, symbolic links, owner, group, and file permissions.

$ rsync -a testdir1/ testdir2

$ ls -al ./testdir2/

Rsync directory with the “-v” option:

To show the rsync process on the terminal “-v” option is used with the rsync command.

$ rsync -anv testdir1/ testdir2

To check the difference that comes by using a “/” at the end of the source directory, write out this command:

$ rsync -anv testdir1 testdir2

The output clearly states that now the source directory is itself transferred.

Rsync directory with the “-P” option:

The “-P” shows the progress of the syncing directory by displaying the progress bar on the terminal.

$ rsync -azP testdir1 testdir2

You can also utilize the same command for resuming any interrupted transfers.

$ rsync -azP testdir1 testdir2

To check if using the same rsync command only sync the modification we made in the directory, create some test files in your source directory.

$ touch testdir1/file{1..10}

After that, execute the same rsync command and see the intelligent behavior of the rsync command.

$ rsync -azP testdir1 testdir2

2. Rsync directory from local to the remote system:

The operation of syncing a local directory to any remote system is known as “push,” as it pushes the directory from your local system to the remote one.

Follow the syntax of push operation for syncing a directory:

$ rsync -a ~/Source username@remote_host:Destination

In our case, we are going to sync the “testdir1” directory to the remote host “10.0.2.15”.

$ rsync -a ~/testdir1 linuxhint@10.0.2.15:testdir2

3. Rsync directory from remote to local system:

As we all know, the opposite of push is “pull.” In the context of syncing, pull operation sync a directory from the remote system to your local system.

$ sudo rsync -v username@remote_host:Source Destination

$ sudo rsync -v linuxhint@10.0.2.15:/home/linuxhint/testdir1 /testdir2

Conclusion:

Rsync is a tool that can improve the reliability of local directory syncing and the file transfer process over remote systems. You can use the rsync command to create complex backups and smooth control over what and how a directory will sync. In this post, we have shown you the various forms of using rsync, which include: rsync directory within a local system, rsync directory from local to a remote system, and also from remote to the local one.