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.
Although the rsync utility comes pre-installed in most Linux systems, you can install it by running the following command:
sudo apt install rsync -y
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:
sudo rsync -av source_path/ target_path/
- 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:
sudo rsync -av ~/Downloads ~/Documents
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:
rsync -av -e ssh user@remote_host:/path/to/source/ /path/to/target/
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:
rsync -av –delete /path/to/source/ /path/to/target/
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:
rsync -av –exclude ‘filename’/path/to/source/ /path/to/target/
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:
rsync -av –dry-run /path/to/source/ /path/to/target/
For example, perform a dry run before synchronizing the files from the “Downloads” directory to the “Documents” directory:
rsync -av –dry-run ~/Downloads ~/Documents
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:
rsync -av –progress source/ target/
Taking the previous example with the progress indicator, you’ll get the result as shown in the following image:
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.