SoFunction
Updated on 2025-03-03

Detailed explanation of using rsync command on LINUX system

LINUX uses rsync command

rsync is a powerful file synchronization tool that can efficiently transfer files between different systems. It provides a wide range of options that allow users to customize transmission specifications according to their specific requirements.

One of the main advantages of using Rsync is that it supports incremental backups. This means that only changes made to the file since the last backup will be transferred, reducing the time and bandwidth required for the backup. In addition, Rsync can be used with different types of shells and supports various protocols such as ssh and ipsec for secure transport.

Rsync also supports compression and decompression methods when sending and receiving data, reducing bandwidth usage. It can even copy devices, groups, links, owners, and permissions, making it a versatile tool for file transfer.

To help users get started with Rsync on Linux, a variety of standard methods and command examples are provided. In addition, various Linux systems are installed instructions, so you can easily get started with this powerful file synchronization tool.

If you want to install Rsync on a Linux system, this is a fairly simple process. The following outlines the steps required to install Rsync on some of the most common Linux distributions.

For Arch Linux-based systems, Rsync can be installed using the following command:

sudo pacman -Sy rsync

For Fedora and RHEL based systems, use the following command:

sudo dnf install rsync

If you are running a Debian or Ubuntu-based system, you can install Rsync using the following command:

sudo apt install rsync

For openSUSE-based systems, use the following command:

sudo zypper install rsync

On Alpine Linux, use the following command:

apk add rsync

If using Gentoo, use the following command:

sudo emerge -av rsync

It is worth noting that the commands provided may vary depending on the version of the Linux distribution used. However, in most cases, installing Rsync is an easy process that can be done using a package manager.

Once Rsync is installed, you can start using it to synchronize files and folders between different systems. With its powerful features and options, Rsync is a valuable tool that can help keep files organized and up to date.

Rsync command syntax

rsync is a powerful file synchronization tool that can be used to efficiently transfer files between different systems.

Its syntax is very simple and runs in a similar way to other file transfer tools such as CP, SCP, and SSH.

Here is a breakdown of the basic Rsync syntax:

Function Syntax example
Local synchronization rsync {options} {Source} {Destination}
Remote synchronous pull rsync {options} <User_Name>@<Remote-Host>:<Source-File-Dir> <Destination>
Remote synchronous push rsync {options} <Source-Files-Dir> <User_Name>@<Remote-Host>:<Destination>

In the above syntax, "Source" refers to the file or directory to be transferred, and "Destination" is the location where the file is to be transferred. For remote synchronization, you need to specify the remote host or IP address, as well as the username and source directory.

Additionally, you can use various options to customize the behavior of Rsync. Here are some of the most commonly used options:

Options describe
-A Archive mode (Permissions, ownership, and timestamps reserved)
-v Detailed output
-z Compress data during transmission
-delete Delete files that do not exist in the source from the destination
-exclude Exclude files from transfer
-P Show progress during transmission
-H Output in a human-readable format

Rsync command options

Rsync is a powerful file synchronization tool with a lot of options to control how to apply and use synchronization software. These options are represented as commands that can be used to customize Rsync behavior.

Here are some of the most common Rsync commands and their uses:

Order Purpose
-a, --archive Archive files and directories when synchronizing. The -a option is equivalent to the following options: -rlptgoD
-b, --backup Backup during synchronization
-l, --links Copy the symbolic link as a symbolic link during synchronization
-d, --delete Delete unrelated files from the target location
-e, --rsh=COMMAND Referring to remote shell to be used in Rsync
-h, --human-readable Display output numbers in human-readable format
-u, --update If the target file is newer, do not copy the file from the source to the target.
-r, --recursive Recursively synchronize files and directories
-n, --dry-run Trial run asynchronously
-p, --progress Displays synchronization progress during transmission
-z, --compress Compress file data during transmission
-q, --quiet Suppress message output
-v, --verbose Detailed output

By using these commands, the behavior of Rsync can be customized to specific needs. For example, using the -a command allows files and directories to be archived when synchronized, while using the -u command will prevent Rsync from copying files from source to target (if the target file is newer).

Overall, with its many powerful commands and options, Rsync is a versatile tool that can be used to efficiently synchronize files and directories between different systems.

Rsync command command example

In this section, some common examples of using the Rsync command are introduced and detailed explanations of its functions and abbreviations.

Although some commands may overlap, these examples are intended to provide a comprehensive overview of the various scenarios where Rsync can be used.

Basic syntax of Rsync command

The rsync command is a powerful tool for efficient transfer and synchronization of files between systems.

The basic syntax is as follows:

rsync [options] source destination
  • rsync: This is the command used to call the rsync tool.
  • options: These are optional parameters that can be passed to a command to modify its behavior. Examples of options include -v verbose output and -r for recursive copying.
  • Source: This is the source file or directory to be copied.
  • Target: This is the target file or directory to which the source data is to be copied.

Use the Rsync command to transfer files and directories

The rsync command provides a powerful and effective way to transfer files and directories between systems. To use rsync for file transfer, you must specify the source path and the destination path in the command.

For example, if you want to copy a file named , from your local computer to a remote server, you can use the following command:

rsync  user@remote:/path/to/destination

In this example, the rsync command transfers the file to the remote server specified by the user name user and the remote path /path/to/destination.

Mirror data using the Rsync command

A common use of the rsync command is to mirror data between two directories, ensuring that the target directory is an exact copy of the source directory.

This can be done using the –delete option, which instructs rsync to delete any files that do not exist in the source directory in the target directory.

To use rsync to mirror data, you can use the following command:

rsync -avz --delete /path/to/source/ user@remote:/path/to/destination

In this example, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission.

The -z or –compress option is used to compress file data during transmission and reduce bandwidth usage.

Finally, the -delete option ensures that all files that do not exist in the source directory in the target directory are deleted.

In general, mirroring data using rsync command is an efficient and reliable way to ensure that the contents of the target directory match the contents of the source directory.

In particular, the –delete option is a powerful feature that can be used to keep two directories synchronized and prevent duplicate or outdated files.

Synchronize files using rsync command

The rsync command can be used to synchronize files between two directories, ensuring that the latest version of files exists in both locations.

This can be done using the –update option, which instructs rsync to copy files from the source directory to the destination directory only if the source file is newer than the corresponding file in the destination directory.

To synchronize files using rsync, you can use the following command:

rsync -avz --update /path/to/source/ user@remote:/path/to/destination

In this example, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress file data during transmission and reduce bandwidth usage. Finally, the -update option ensures that only newer files from the source directory are copied to the destination directory.

Synchronizing files with rsync is an effective way to ensure that the latest version of the file exists in both locations, making it a popular choice for backup and mirroring applications. In particular, the –update option is a useful feature that saves time and bandwidth by copying only files that have been changed since the last sync.

Exclude files and directories from Rsync transfers

When using rsync to transfer files, you may want to exclude certain files or directories for transfer. This can be implemented using the –exclude option, which allows you to specify patterns that match the files or directories you want to exclude.

To exclude files or directories from rsync transfers, you can use the following command:

rsync -avz --exclude '*.log' /path/to/source/ user@remote:/path/to/destination

In this example, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress file data during transmission and reduce bandwidth usage. Finally, the -exclude '*.log' option specifies a pattern that matches any files with the .log extension that will be excluded from the transfer.

Using the –exclude option is a powerful feature that can be used to fine-tune rsync transfers and ensure that only necessary files are transferred. This can save time and bandwidth, especially when transferring large numbers of files or directories.

Use the Rsync command to transfer large files

When transferring large files using the rsync command, the –partial option is a useful feature for efficient and reliable transfer of files. The –partial option allows only the remainder of the file to resume interrupted transfers.

To use rsync to transfer large files, you can use the following command:

rsync -avz --partial largefile user@remote:/path/to/destination

In this example, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress file data during transmission and reduce bandwidth usage. Finally, the -partial option allows interrupted transfers to be restored by transferring only the remainder of the file.

Using the –partial option is a useful feature, especially when transferring large files over slow or unreliable network connections. With this option, you can restore transmissions that are interrupted due to network interruptions without the need to start the entire transmission from scratch. This saves time and bandwidth, ensuring fast and reliable transfer of large files.

Compressed transfer files

Transferring files through compression can greatly reduce the amount of data that needs to be transferred over the network, and the rsync command provides a simple and effective way to achieve this using the -z or –compress options.

To use rsync to transfer compressed files, you can use the following command:

rsync -avz /path/to/source/ user@remote:/path/to/destination

In this example, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network.

Using the –compress option is a useful feature, especially when transferring large files or using slow or limited network connections. Through compression, data transmission speeds are faster, resulting in faster and more efficient file transfer. However, it is important to note that compression increases CPU load on both the source and target systems, so the benefits of compression must be balanced with the performance of the system involved.

Encrypted transfer of files

The rsync command provides a way to encrypt files, which helps ensure the security of data transmitted over the network. This can be done by specifying the encryption method using the -e option.

One of the most commonly used encryption methods for rsync is SSH, which can be specified using the following command:

rsync -avz -e ssh /path/to/source/ user@remote:/path/to/destination

In this example, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network. Finally, the -e ssh option specifies SSH encryption.

Encryption with rsync is a useful feature that helps ensure sensitive data is safely transmitted over the network. When using SSH encryption, data is encrypted before being transmitted over the network, making it difficult for an attacker to intercept or read data. However, it is important to ensure that the systems involved in the transmission are properly protected and appropriate security measures are taken to protect the data being transmitted.

Reserve the permissions and ownership of the files:

To ensure that correct access is retained during transfer, the rsync command provides a way to preserve file permissions and ownership. This is achieved by using the --perms and --owner options.

To retain permissions and ownership information of files during rsync transfer, you can use the following command:

rsync -avz --perms --owner /path/to/source/ user@remote:/path/to/destination

In this example, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network. Finally, the -perms and -owner options are used to retain permissions and ownership information for the file.

When transferring files between systems with different user and group permissions, it is crucial to use the –perms and –owner options to ensure that the correct permissions and ownership information is retained. This helps prevent file access and security issues, ensuring that transferred files can be used as expected on the target system.

Transfer files using detailed output

The rsync command provides a way to get more details about the transfer by using the -v or –verbose option. This option enables detailed output, displays the progress of the transfer and any errors or warnings that occur.

To use rsync to transfer files with verbose output, you can use the following command:

rsync -avz -v /path/to/source/ user@remote:/path/to/destination

In this example, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network.

Using the -v or –verbose option is a useful feature that can help ensure that the transfer is performed as expected and to quickly detect and resolve any errors or warnings. The detailed output can provide detailed information about the transmission, including the number of files transferred, the transmission rate, and any errors or warnings during the transmission. This information can be useful when transferring large amounts of data or using slow or unreliable network connections.

Test the Rsync command with Dry Run

Executing a trial run using the rsync command is useful for testing the command and ensuring that the correct file is transferred without overwriting any important data. The trial run allows the simulation and preview of what will be transferred without copying any files.

To use the trial run option with rsync, add the -n or –dry-run option to the command.

For example, you can use the following command to perform a trial run of rsync transfer:

rsync -avzn /path/to/source/ user@remote:/path/to/destination

In this example, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network. Finally, the -n or –dry-run option simulates transfer without copying any files.

Performing a trial run before actual transmission can check the transmission before execution, helping to prevent accidental data loss or corruption. This is especially useful when dealing with large or critical files, as data loss can have serious consequences.

There is bandwidth limitation when transferring files

When transferring large files or directories over the network, it is important to limit the bandwidth used by the transmission to avoid network saturation and affect other users. To limit the bandwidth used, you can use the –bwlimit option in conjunction with the rsync command.

To use rsync to transfer bandwidth-limited files, you can use the following command:

rsync -avz --bwlimit=500 /path/to/source/ user@remote:/path/to/destination

In this example, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network. Finally, the -bwlimit option limits bandwidth to 500 kb per second.

Limiting the bandwidth used by the rsync command is particularly useful when using slow or congested networks, or when using large files that may saturate the network and affect other users. By setting bandwidth limits, you can ensure that transmission does not negatively affect other network activities while still completing transmission within a reasonable time.

Transfer files using recovery options

If the transmission is interrupted or stopped for any reason, the transmission can be resumed from the interrupt using the rsync command with the –partial and –progress options.

To use rsync to transfer files through the recovery option, you can use the following command:

rsync -avz --partial --progress /path/to/source/ user@remote:/path/to/destination

In this example, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network. Finally, the -partial and -progress options are used to restore interrupted transmissions and display progress information during transmission.

The –partial option indicates rsync to retain partially transferred files, while the –progress option shows the progress of the transfer, including the amount of data transmitted, the transfer rate, and the estimated remaining time.

Use recovery options in conjunction with rsync is especially useful when transferring large files over an unreliable network or when transfers may be interrupted for any reason. By restoring transmission from an interrupt, it is possible to avoid restarting transmission, saving time and bandwidth.

Transfer files using IPv4 or IPv6 protocols

The rsync command supports IPv4 and IPv6 protocols for network communication. To specify a specific protocol, you can use the -4 or -6 options with the rsync command.

To use rsync to transfer files over IPv4 or IPv6 protocols, you can use the following command:

rsync -avz -4 /path/to/source/ user@remote:/path/to/destination

In this example, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network. Finally, the -4 option is used to specify the IPv4 protocol for network communication.

The -4 option tells rsync to use the IPv4 protocol for network communication. If you want to use the IPv6 protocol, you can use the -6 option.

By default, rsync will try to use the protocol that is best suited for network communication. However, specifying a specific protocol can be useful when you want to ensure that the transmission uses a specific protocol or that the network configuration may limit the available protocol.

Transfer files using Rsync via SSH

To transfer files over an SSH connection using rsync, you can use the -e option to specify the SSH command to use, such as ssh. However, if you want to specify the username for the SSH connection, you can use the user@host syntax.

For example, the following command uses the default SSH command to transfer files over SSH:

rsync -avz -e ssh /path/to/source/ user@remote:/path/to/destination

In this command, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network. Finally, the -e ssh option is used to specify that the transfer should be performed over an SSH connection.

On the other hand, the following command uses the SSH command with the -l option to specify the username of the SSH connection:

rsync -avz -e "ssh -l user" /path/to/source/ remote:/path/to/destination

In this command, the -l user option specifies the user name of the SSH connection. This option is passed as a parameter to the ssh command and is specified using the -e option. By using -l user instead of user@remote, it specifies the username as an option to the SSH command, which is useful if the remote host does not allow the SSH username to be specified in the user@host format.

Both commands are designed to transfer files over an SSH connection using rsync. However, the second option specifies the SSH username more explicitly and is useful when the first option is not possible.

Transfer files using remote shell

When transferring files over the network, you sometimes need to use a remote shell to access the target computer. The rsync command provides the ability to transfer files using a remote shell, which can be specified using the –rsh option.

For example, the following command uses an SSH remote shell with the specified username "user" to transfer files:

rsync -avz --rsh="ssh -l user" /path/to/source/ remote:/path/to/destination

This command uses an SSH remote shell to transfer the contents of the source directory to the destination directory. The -a or –archive option is used to preserve file properties, while the -v or –verbose option provides detailed output during transfer. The -z or –compress option compresses data during transmission, reducing the amount of data that needs to be transmitted over the network. Finally, the -rsh option specifies the remote shell command, in this case the SSH command, and the remote username is specified using the -l option.

This is useful when direct access to the target computer or when using a remote shell to provide additional security measures.

Use Rsync for backup

Backing up important files and directories is a critical task, and the rsync command can be an effective tool to achieve this. To create periodic backups using rsync, you can use the -a or –archive option, which retains permissions, ownership, and timestamps of the file. This option ensures that the backup is an exact copy of the source directory, making it easier to recover data in the event of a failure or loss.

To perform a backup using rsync, you can use the following command:

rsync -avz /path/to/source/ /path/to/backup

In this command, the -a or –archive option is used to retain permissions, ownership, and timestamp information of the file during transmission. The -v or –verbose option provides detailed output during transmission, while the -z or –compress option compresses data during transmission, reducing the amount of data that needs to be transmitted over the network.

This command creates a backup of the source directory in the backup directory, retaining the permissions, ownership, and timestamps of the file. It is important to note that the target directory should be located in a different device or location than the source directory to avoid losing both in case of system failure.

Regular backups with rsync can help keep important files and directories safe and easily recovered in disasters.

Transfer files using multithreading

To optimize the transfer of large files or directories, the rsync command can use the –parallel option to enable multithreading. This option allows rsync to transfer multiple files simultaneously, thereby increasing the transfer speed.

To use rsync to transfer files over multithreading, use the following command:

rsync -avz --parallel=4 /path/to/source/ user@remote:/path/to/destination

In this example, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network. Finally, the –parallel=4 option is used to specify the number of parallel threads to use.

The –parallel option allows specifying the number of parallel threads used for transmission. The optimal number of threads depends on the available network bandwidth, the size of files being transferred, and other factors. It is recommended to try different values ​​to find the best number for the specific situation.

Multithreading can greatly improve the speed of rsync transfers, especially when transferring large files or directories over the network. However, it is important to note that using too many threads can also have a negative impact on performance, as each thread requires resources and increases network load.

Transfer files using Rsync daemon

The rsync daemon is a useful alternative to using SSH to transfer files, as it allows files to be transferred between systems without an SSH connection. To use the rsync daemon, you can start the service on a remote system and then transfer the files using the rsync:// protocol.

To transfer files using the rsync daemon, you can use the following command:

rsync -avz /path/to/source/ rsync://remote/destination

This command uses the rsync daemon to transfer the contents of the source directory to the destination directory. The -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network.

The target in this command is specified using the rsync:// protocol, followed by the remote system's host name and destination directory. By default, the rsync daemon listens for TCP port 873, so you must make sure that the port is open on the remote system.

The rsync daemon has several advantages over SSH in file transfer, including faster transfer speeds and less remote system overhead. However, it requires additional setup and configuration, so it may not be the best choice for all cases.

Transfer files using Rsync module

The rsync command can be used with modules to access logical groupings of files and directories that can be accessed by remote systems. To use a module with rsync, you can specify the module name in the rsync command. Here is an example:

Use the Rsync module to transfer files:

rsync -avz rsync://remote/module/ /path/to/destination

In this command, the rsync daemon runs on a remote system and is configured with a module called "module". The contents of this module will be transferred to the target directory on the local system.

The -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network.

Using modules with rsync is an effective way to organize and transfer files between systems. Access and transfer of specific data sets can be simplified by grouping files and directories into modules.

Use Rsync with Crontab

Automatic backup or file transfer is an important task, and rsync can be used with the crontab utility to schedule regular rsync commands. Crontab is a time-based scheduling utility in Linux and Unix-like operating systems.

To automate rsync backup or transfer, use the crontab -e command to edit the crontab file and add the rsync command. The command must contain the required options and parameters, such as the source directory, the destination directory, and any other options required for transfer.

For example, the following command will set up a crontab to run the rsync command every day at midnight:

0 0 * * * rsync -avz /path/to/source/ /path/to/backup

In this example, crontab is set to run 0 minutes after midnight every day. The rsync command uses the -a or –archive option to retain permissions, ownership, and timestamp information, and provides detailed output during transmission using the -v or –verbose option. The -z or –compress option is used to compress data during transmission and reduce the amount of data to be transmitted. The source directory is specified as /path/to/source/ and the backup directory is specified as /path/to/backup/.

Using the crontab utility with rsync ensures periodic backups or transfer of important files without manual intervention.

Transfer files using batch mode

Transferring large amounts of files using rsync can be a daunting task, but the command provides an easy way to accomplish this using batch mode. The –files-from option can be used to specify a list of files to be transferred. This option is especially useful when you want to transfer only a specific file or directory instead of the entire source directory.

Here is a sample command that demonstrates how to use batch mode with rsync:

rsync -avz --files-from= /path/to/source/ user@remote:/path/to/destination

In this command, the -a or –archive option is used to retain permissions, ownership, and timestamp information during transmission, while the -v or –verbose option provides detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network. Finally, the -files-from option is used to specify a list of files to be transferred, which is stored in the file.

To create a file, you can use any text editor to create a list of files to be transferred, one file per line. After the file is created, you can pass it to the rsync command using the --files-from option, as shown in the example command.

By using batch mode with rsync, large amounts of files can be transferred more efficiently and efficiently without the need to transfer unnecessary files or directories.

Transfer files using custom logging

To monitor the progress of rsync transfers and catch any errors or warnings, you can use the --log-file option to create a custom log file. This is especially useful when solving problems or auditing transmission activities.

Here is an example of how to use the --log-file option with rsync:

rsync -avz --log-file= /path/to/source/ user@remote:/path/to/destination

In this command, the --log-file option specifies the name and location of the log file to be generated. The -a or –archive option is used to retain file permissions, ownership, and timestamp information during transfer. The -v or –verbose option provides more detailed output during transmission. The -z or –compress option is used to compress data during transmission, reducing the amount of data that needs to be transmitted over the network.

The rsync command transfers the contents of the source directory to the destination directory, and all outputs are recorded to the file. The log file can then be used to view transfer activity, resolve any errors or warnings that occur, or to audit transfer history.

It should be noted that the --log-file option will create a new log file for each transfer. If you want to append the output to an existing log file, you can use the --append option instead of the --log-file option.

Use Rsync with trial runs and custom logging

To simulate rsync transfers without actually copying any files and tracking the output in custom log files, you can use the --dry-run and --log-file options with the rsync command.

The --dry-run option (or -n) allows preview of transfers, showing which files will be transferred and what actions are taken without any changes to the source or destination directory. This is useful for testing and validating the rsync command before transferring files.

The --log-file option (or -l) allows you to specify custom log files to log rsync output, which facilitates auditing and troubleshooting. This option writes to the detailed log of the transfer, including any errors or warnings during the process.

For example, the following command performs a trial run of the rsync command and records the output to a custom file named :

rsync -avzn --log-file= /path/to/source/ user@remote:/path/to/destination

In this command, the -a or –archive option is used to retain permissions, ownership, and timestamp information of the file during transfer, while the -v or –verbose option provides detailed output during transfer. The -z or –compress option compresses data during transmission, reducing the amount of data that needs to be transmitted over the network.

The --dry-run and --log-file options are used to simulate transfers without copying any files and writing output to the file.

Overall, using both the –dry-run and –log-file options can help ensure the accuracy and reliability of rsync transfers while providing detailed information for troubleshooting and auditing purposes.

Comparing files and directories using Rsync

Rsync is a key feature of the tool by comparing files and directories between sources and destinations and transferring only differences. This feature only transfers changes rather than copying entire files or directories, helping save time and bandwidth.

To compare files and directories between source and destination using rsync, you can use the -n or –dry-run option. This option performs empty runs of the command without transferring files. Instead, it displays what will be transferred between the source and the destination.

The following command shows an example of how to use rsync to compare files and directories between sources and targets with the trial run option:

rsync -avn /path/to/source/ /path/to/destination

In this command, the -a or –archive option retains permissions, ownership, and timestamp information for the file during transmission, while the -v or –verbose option provides detailed output during transmission. The -n or –dry-run option performs a trial run of the rsync command to display the content to be transferred without copying any files.

Overall, using the -n or –dry-run option with rsync can help ensure accurate and reliable transmissions, while also helping save time and bandwidth by only transmitting necessary changes.

Transfer files using partial transfer directory

When using rsync to transfer large files or directories, sometimes interrupts or failures occur, resulting in only partial content being transferred. In this case, the –partial-dir option can be used with the rsync command to specify the directory where the partially transferred files are stored.

An example command for transferring files with partial transfer directories is as follows:

rsync -avz --partial-dir=.rsync-partial /path/to/source/ user@remote:/path/to/destination

In this command, the -a or –archive option is used to retain permissions, ownership, and timestamps of the file during transfer, while the -v or –verbose option provides detailed output during transfer. The -z or –compress option compresses data during transmission, reducing the amount of data that needs to be transmitted over the network.

The –partial-dir option specifies the directory in which partially transferred files will be stored. In this case, any partially transferred files will be stored in the .rsync-partial directory.

Use the –partial-dir option to help ensure that files are not lost due to interruptions or failures of transfers, and also makes it easier to resume transfers from interrupt locations.

Copy files to multiple locations using Rsync

Copying files to multiple locations can be a time-consuming task. However, rsync with the –files-from option makes the process more efficient. The –files-from option allows specifying a list of files to be transferred. Using the rsync command for multiple target directories allows these files to be copied to multiple locations simultaneously.

The example command to copy a file to multiple locations is as follows:

rsync -avz --files-from= /path/to/source/ user@remote:/path/to/destination1 user@remote:/path/to/destination2

In the provided example, the -a or –archive option is used to retain permissions, ownership, and timestamp information of the file during transfer, while the -v or –verbose option provides detailed output during transfer. The -z or –compress option compresses data during transmission, reducing the amount of data that needs to be transmitted over the network.

The –files-from option specifies the list of files to be transferred, and the two target directories are specified at the end of the command. The files listed in the file are transferred to the /path/to/destination1 and /path/to/destination2 directories on the remote system.

Overall, using rsync to copy files to multiple locations when managing large amounts of data can help save time and improve efficiency.

Use Rsync to delete files on the target

When using rsync to transfer files, sometimes you need to delete files on the destination that no longer exist on the source. To do this, you can use the –delete option with the rsync command. This option tells rsync to delete all files on the target that do not exist on the source.

The command to transfer files and delete any files on the target that no longer exist on the source is as follows:

rsync -avz --delete /path/to/source/ user@remote:/path/to/destination

In this command, the -a or –archive option is used to retain permissions, ownership, and timestamp information of the file during transfer, while the -v or –verbose option provides detailed output during transfer. The -z or –compress option compresses data during transmission, reducing the amount of data that needs to be transmitted over the network.

The –delete option is used to delete files on the target that no longer exist on the source. Use the –delete option to help keep the target directory synchronized with the source directory, ensuring that both directories have the same file set.

Synchronize files between two servers using Rsync

Rsync can be used to synchronize files between two servers, which is very useful to ensure that the same data is available on both systems. To synchronize files between two servers, use the rsync command with two remote paths.

The following command synchronizes the contents of the source directory on the first remote system with the destination directory on the second remote system:

rsync -avz user1@remote1:/path/to/source/ user2@remote2:/path/to/destination

In this command, the -a or –archive option is used to retain permissions, ownership, and timestamp information of the file during transfer, while the -v or –verbose option provides detailed output during transfer. The -z or –compress option compresses data during transmission, reducing the amount of data that needs to be transmitted over the network.

The first remote path user1@remote1:/path/to/source/ specifies the location of the source directory on the first remote system. Instead, the second remote path user2@remote2:/path/to/destination specifies the location of the target directory on the second remote system.

When you run this command, rsync compares files on the two remote systems and transfers only differences, ensuring that the two systems have the same data.

in conclusion

In short, the rsync command is a powerful tool for transferring files and directories between systems. rsync is able to compare files and directories and transfer only differences, thus providing an effective way to keep files synchronized between systems or perform regular backups. Additionally, rsync offers a wide range of options and features that allow customization to meet specific needs such as bandwidth limitations, transmission compression, encryption, and permission retention.

By mastering the various options and features of rsync, users can easily perform complex file transfers and ensure efficient and reliable data transfer. From transferring files via SSH or rsync daemons to synchronizing files between two servers, rsync is a valuable tool for any system administrator, developer, or user who needs to move files between systems. With its speed, reliability and versatility, rsync becomes a must-have tool for any Linux user.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.