Rsync is a command line utility known for its data synchronization features in Linux. You can use it to synchronize files on the same or different systems. Additionally, it offers file compression, encryption, selective synchronization, and more, making it superior to other tools. It can compare the source and target directories so that only newly added and updated files are transferred from the source directory.
All these features help reduce waiting time and increase work efficiency. However, many Linux users are unfamiliar with rsync's capabilities and do not understand its advantages in efficient file transfer. This article will briefly introduce how to use rsync to efficiently transfer files between directories in a Linux system.
There are several ways to use rsync in Linux, so we will further divide this section to explain its usage in different scenarios.
Rsync installation
While the rsync utility comes preinstalled on most Linux systems, you can install it by running the following command:
sudo apt install rsync—y
He said:
Now, check the installed rsync version.
rsync——version
He said:
Once completed, run the following command to start synchronizing the source and destination:
Rsync-o source target
He said:
Local file transfer
You can use rsync to copy-paste files in the same system with the following command:
Sudo rsync-av source path/destination path/
He said:
For example, let us use it to copy files from the "Downloads" directory to the "Document" directory:
sudo rsync—av~/Download~/Documentation
He said:
Remote file transfer
You can primarily use rsync to transfer files remotely between two machines connected via 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/destination/
He said:
Using the "-e ssh" option, you can tell the system to specifically use secure shell or SSH for this file transfer.
Delete the file from the target directory (does not exist in the source directory)
Suppose you want to make two directories identical and contain similar files. In this case, you must delete the extra files present in the target directory, if any. Fortunately, in rsync you can use the "--Delete" option to do this. To learn how, execute the following command:
rsync—av——delete /path/to/source//path/to/target/
He said:
Delete files during transfer
When transferring an entire directory, you may need to exclude certain files or subdirectories. Therefore, you can use the "--exject" option in the following ways:
rsync-av——exclude 'filename'/path/to/source//path/to/target/
He said:
Dry run
When using rsync for large directories, you should do a test run first. This way the system demonstrates what the command will do without transferring files. However, this can help you prevent any unwanted files from being transferred. To perform a dry run, use the "--dry-run" option in the following command:
rsync-av-dry run /path/to/source//path/to/target/
He said:
For example, before synchronizing files from the "Downloads" directory to the "Documents" directory, perform a dry run:
Rsync-av–Walkthrough~/Download~/Documentation
He said:
Show progress indicator
Since some users like to use the progress indicator to see the progress of their transfer, you can enable it using the following command:
rsync—av——progress source/target/
He said:
Taking the previous example and using the progress indicator, you will get the result as shown below:
Rsync is a powerful tool in Linux for transferring files between directories. This blog explains its various use cases such as local and remote data synchronization. Additionally, it has multiple subcommands to facilitate features such as excluding files during transfer and deleting files from the destination. Despite these features, users can still make mistakes. Therefore, you should always perform a staging for large file transfers.
The above is the detailed content of How to efficiently transfer files between directories using rsync in Linux. For more information, please follow other related articles on the PHP Chinese website!