Rsync is a command line utility that is famous for its data synchronization features in Linux. You can use it to synchronize the files on the same and different systems. Moreover, it offers features like file compression, encryption, selective synchronization, and more which make it superior to other tools. It can compare the source and target directories to transfer only the newly added and updated files from the source directory.
All of these features help reduce the wait time and enhance productivity. However, many Linux users don’t know the features of rsync are unfamiliar with its efficient file transfer. This blog will briefly explain the methods to use rsync for efficient file transfer between directories in Linux.
How to Use Rsync for Efficient File Transfer between Directories in Linux
There are a few ways to use rsync in Linux, so we will divide this section further to explain its usage in different scenarios.
Rsync Installation
Although the rsync utility comes pre-installed in most Linux systems, you can install it by running the following command:
Now, check the installed version of rsync.
Once you are done, run the following command to start syncing the source and the target:
-
- The source is the directory from which you want to synchronize the files.
- Target is your destination directory where you want to store those files.
Local File Transfer
You can use rsync to copy-paste the files within the same system using the following command:
-
- The “-a” option stands for archive which preserves the file attributes during a transfer.
- The “-v” is for the verbose mode in which you can see what files are being transferred.
For example, let’s use it to copy the files from the “Downloads” directory to the “Document” directory:
Remote File Transfer
You can mainly use rsync to transfer the files remotely between two machines that are connected over a network. For this, you need to specify the remote host using the following given syntax:
With the “-e ssh” option, you can tell the system to specifically use the secure shell or SSH for this file transfer.
Delete Files from the Target Directory (Which Are Not Present in the Source)
Suppose you want to make both directories identical and contain similar files. In this case, you have to delete the extra files (if any) present in the target directory. Fortunately, with rsync, you can do this using the “– –delete” option. To know how, go through the following command:
Exclude Files during Transfer
While transferring whole directories, you may need to exclude some files or subdirectories. Therefore, you can use the “– –exclude” option in the following way:
Dry Run
When using rsync for large directories, you should go for a dry run first. This way, the system demonstrates what this command would do without transferring the files. However, this can help you prevent transferring any unwanted files. To perform a dry run, use the “– –dry-run” option in the following command:
For example, perform a dry run before synchronizing the files from the “Downloads” directory to the “Documents” directory:
Display the Progress Indicator
Since some users prefer to have a progress indicator to see the progress of their transfer, you can enable it using the following command:
Taking the previous example with the progress indicator, you’ll get the result as shown in the following image:
Conclusion
Rsync is a powerful tool to transfer the files between directories in Linux. This blog explains its various use cases such as local and remote data synchronization. Furthermore, it features multiple subcommands to facilitate some functionalities like excluding the files during transfer and deleting the files from the destination. Despite all these functions, the users can still make mistakes. Hence, you should always perform a dry run for big file transfers.