In this tutorial, we will show you how to use the Rsync command on Linux. For those of you who didn’t know, Rsync is a powerful and versatile command-line tool for synchronizing files and directories between local and remote systems. It is widely used for backups, migrations, and mirroring operations. Rsync works by comparing the source and destination directories and only transferring the differences. This means that only the changes to the files and directories are transferred, which can save a significant amount of time and bandwidth, especially for large files.
Rsync Features
Here are some of the key features and benefits of using rsync:
- Incremental backups: Rsync can be used to perform incremental backups of files and directories, which means that only the changes made since the last backup are transferred. This can save a significant amount of time and disk space.
- Remote transfers: Rsync can be used to transfer files and directories between local and remote systems over a network connection, which can be useful for backing up data to a remote server.
- Bandwidth optimization: Rsync uses a delta-transfer algorithm that only transfers the changes to a file, rather than the entire file. This can save bandwidth and reduce transfer times, especially for large files.
- Error recovery: Rsync has built-in error recovery features that allow it to continue transferring files even if there are interruptions or errors in the transfer.
- Compression: Rsync can compress files during transfer, which can further reduce transfer times and bandwidth usage.
Install Rsync on Linux
Installing Rsync on Linux is usually straightforward as it is pre-installed on most Linux distributions. However, if it is not installed, it can be installed using the default package manager of the respective Linux distribution.
On Ubuntu and Debian-based, you can use the following command to install Rsync:
sudo apt install rsync
On CentOS and Fedora, Rocky Linux, you can use the following command to install Rsync:
sudo dnf install rsync
Basic Usage of Rsync Command
The Rsync command is a powerful tool for file transfer and synchronization between local and remote systems. It uses the SSH protocol by default, but it can also work with other protocols like FTP, SFTP, and more. Here is the basic syntax of the Rsync command:
rsync [options] source destination
In this syntax, source
refers to the path of the file or directory that you want to copy, and destination
refers to the target location where you want to copy the file or directory. They options
are additional parameters that you can use to modify the behavior of the Rsync command.
Rsync Command Options
The Rsync command has several options that you can use to customize its behavior. Here are some of the most commonly used options:
Option | Description |
---|---|
-a | Archive mode; preserves permissions, timestamps, and other attributes |
-v | Verbose mode; displays detailed output |
-z | Compresses data during transfer |
--delete | Deletes files in the destination that are not present in the source |
-P | Shows progress during transfer and resumes partial transfers |
--exclude | Excludes files or directories from the transfer |
--dry-run | Simulates the transfer without actually copying any files |
You can use these options by adding them to the Rsync command line. For example, the following command uses the -a
, -v
, and -z
options to copy a directory from a local system to a remote system:
rsync -avz /path/to/local/directory/ user@remote:/path/to/remote/directory/
This command uses the archive
mode to preserve permissions, timestamps, and other attributes of the files and directories. The verbose
mode displays detailed output and the compress
option compresses the data during transfer to reduce the amount of data that needs to be transferred.
Example of Using Rsync Command
Here is an example of how you can use the Rsync command to copy a directory from a local system to a remote system:
Suppose you have a directory named my-files
on your local system located at /home/user/my-files
, and you want to copy it to a remote system with the IP address 192.168.77.20
and the username remoteuser
. You want to copy the directory to the /home/remoteuser
directory on the remote system. To do this, you can use the following command:
rsync -avz /home/user/my-files/ remoteuser@192.168.77.20:/home/remoteuser/
This command uses the archive
mode to preserve the permissions, timestamps, and other attributes of the files and directories. The verbose
mode displays detailed output, and the compress
option compresses the data during transfer to reduce the amount of data that needs to be transferred.
Conclusion
The Rsync command is a powerful tool for file transfer and synchronization between local and remote systems. With its many options and features, it can be customized to suit a variety of use cases. By following the basic syntax and using the available options, you can easily copy and synchronize files and directories between systems.