How to use Rsync Command on Linux

Rsync is a powerful file synchronization tool that can efficiently transfer files between different systems. It offers a wide range of options that allow users to customize the transfer specifications based on their specific requirements.

One of the key benefits of using Rsync is its support for incremental backups. This means that only the changes made to a file since the last backup will be transferred, reducing the time and bandwidth required for backups. Additionally, Rsync can be used with different types of shells and supports various protocols like ssh and ipsec for secure transmission.

Rsync also supports compression and decompression methods while sending and receiving data, reducing the amount of bandwidth used. It can even copy devices, groups, links, owners, and permissions, making it a versatile tool for file transfers.

To help users get started with Rsync on Linux, several standard methods and command examples are available. Additionally, installation instructions are provided for various Linux systems, making it easy to get started with this powerful file synchronization tool.

Install Rsync On Linux

If you’re looking to install Rsync on a Linux system, you’ll be pleased to know that it’s a reasonably straightforward process. Below, we’ve outlined the steps required to install Rsync on some of the most common Linux distributions.

For Arch Linux-based systems, you can install Rsync using the following command:

sudo pacman -Sy rsync

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

sudo dnf install rsync

If you’re running a Debian or Ubuntu-based system, you can use the following command to install Rsync:

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 you’re using Gentoo, use the following command:

sudo emerge -av rsync

It’s worth noting that the commands provided may differ depending on the version of the Linux distribution you’re using. However, in most cases, installing Rsync is a straightforward process and can be accomplished using your distribution package manager.

Once you’ve installed Rsync, 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 you keep your 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 quite straightforward and operates in a similar style to other file transfer tools like CP, SCP, and SSH.

Here’s a breakdown of the basic Rsync syntax:

FunctionSyntax Example
Local Syncrsync {options} {Source} {Destination}
Remote Sync Pullrsync {options} <User_Name>@<Remote-Host>:<Source-File-Dir> <Destination>
Remote Sync Pushrsync {options} <Source-Files-Dir> <User_Name>@<Remote-Host>:<Destination>

In the above syntax, the “Source” refers to the file or directory you want to transfer, while the “Destination” is the location where you want to transfer the files. For remote syncs, you’ll 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:

OptionDescription
-aArchive mode (preserves permissions, ownership, and timestamps)
-vVerbose output
-zCompress data during the transfer
–deleteDeletes files from the destination that are not present in the source
–excludeExclude files from the transfer
-PShows progress during transfer
-hOutput in human-readable format

Rsync Command Options

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

Here are some of the most common Rsync commands, along with their purposes:

CommandPurpose
-a, --archiveArchive files and directories while synchronizing. The -a option is equal to the following options: -rlptgoD
-b, --backupTake a backup during synchronization
-l, --linksCopy symlinks as symlinks during the sync
-d, --deleteDeletes extraneous files from the destination location
-e, --rsh=COMMANDMention the remote shell to use in Rsync
-h, --human-readableDisplay the output numbers in a human-readable format
-u, --updateDon’t copy files from source to destination if the destination files are newer.
-r, --recursiveSync files and directories recursively
-n, --dry-runPerform a trial run without synchronization
-p, --progressShow the sync progress during the transfer
-z, --compressCompress file data during the transfer
-q, --quietSuppress message output
-v, --verboseVerbose output

By using these commands, you can tailor Rsync’s behavior to your specific needs. For example, using the -a command will allow you to archive files and directories while synchronizing, while using the -u command will prevent Rsync from copying files from the source to the destination if the destination files are 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 Examples

In this section, we will cover some common examples of using Rsync commands and provide detailed explanations of their functions and abbreviations. Although some of the commands may overlap, the examples aim to provide a comprehensive overview of the various scenarios in which Rsync can be used.

Basic Syntax of Rsync Command:

The rsync command is a powerful tool for efficiently transferring and synchronizing files between systems. Its basic syntax is as follows:

rsync [options] source destination
  • rsync: This is the command used to invoke the rsync tool.
  • options: These are optional arguments that can be passed to the 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 that you want to copy.
  • destination: This is the destination file or directory where you want to copy the source data.

Transfer Files and Directories with Rsync Command:

The rsync command provides a powerful and efficient way to transfer files and directories between systems. To use rsync for file transfers, you must specify the source and destination paths in the command. For instance, if you want to copy a file named example.txt from your local machine to a remote server, you can use the following command:

rsync example.txt user@remote:/path/to/destination

In this example, the rsync command will transfer the file example.txt to the remote server specified by the username user and the remote path /path/to/destination.

Mirror Data with Rsync Command:

One common use of the rsync command is to mirror data between two directories, ensuring that the destination directory is an exact replica of the source directory. This can be achieved using the –delete option, which instructs rsync to delete any files in the destination directory that do not exist in the source directory.

To mirror data using rsync, 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 preserve permissions, ownership, and timestamp information during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option is used to compress the file data during transfer, reducing the amount of bandwidth used. Finally, the –delete option ensures that any files in the destination directory that do not exist in the source directory are deleted.

Overall, using the rsync command to mirror data is an efficient and reliable way to ensure that the contents of the destination directory match those of the source directory. The –delete option, in particular, is a powerful feature that can be used to keep two directories in sync and prevent duplicate or outdated files.

Synchronize Files with Rsync Command:

The rsync command can be used to synchronize files between two directories, ensuring that the most recent versions of files are present in both locations. This can be achieved 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 preserve permissions, ownership, and timestamp information during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option is used to compress the file data during transfer, reducing the amount of bandwidth used. Finally, the –update option ensures that only files that are newer in the source directory are copied to the destination directory.

Using rsync to synchronize files is an efficient way to ensure that the most recent versions of files are present in both locations, making it a popular choice for backup and mirroring applications. The –update option, in particular, is a useful feature that can be used to save time and bandwidth by copying only the files that have changed since the last synchronization.

Exclude Files and Directories from Rsync Transfer:

When transferring files with rsync, you may want to exclude certain files or directories from being transferred. This can be achieved using the –exclude option, which allows you to specify a pattern that matches the files or directories you want to exclude.

To exclude files or directories from the rsync transfer, 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 preserve permissions, ownership, and timestamp information during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option is used to compress the file data during transfer, reducing the amount of bandwidth used. Finally, the –exclude ‘*.log’ option specifies a pattern that matches any files with the .log extension, which will be excluded from the transfer.

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

Transfer Large Files with Rsync Command:

When transferring large files with the rsync command, the –partial option is a useful feature that can be used to transfer files efficiently and reliably. The –partial option allows you to resume an interrupted transfer by transferring only the remaining part of the file.

To transfer large files using rsync, 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 preserve permissions, ownership, and timestamp information during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option is used to compress the file data during transfer, reducing the amount of bandwidth used. Finally, the –partial option allows you to resume an interrupted transfer by transferring only the remaining part of the file.

Using the –partial option is a useful feature that can be particularly helpful when transferring large files over slow or unreliable network connections. With this option, you can resume a transfer that has been interrupted due to a network outage, without having to restart the entire transfer from the beginning. This can save time and bandwidth, ensuring that large files are transferred quickly and reliably.

Transfer Files with Compression:

Transferring files with compression can greatly reduce the amount of data that needs to be transmitted over a network, and the rsync command provides a simple and efficient way to do this using the -z or –compress option.

To transfer files with compression using rsync, 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 preserve permissions, ownership, and timestamp information during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option is used to compress the data during transfer, reducing the amount of data that needs to be transmitted over the network.

Using the –compress option is a useful feature that can be particularly helpful when transferring large files or when working with slow or limited network connections. With compression, the data is transmitted more quickly, resulting in faster and more efficient file transfers. However, it’s important to note that compression can increase the CPU load on both the source and destination systems, so it’s essential to balance the benefits of compression with the performance of the systems involved.

Transfer Files with Encryption:

The rsync command provides a way to transfer files with encryption, which helps to ensure that the data being transmitted over the network is secure. This can be achieved using the -e option to specify the encryption method.

One of the most commonly used encryption methods with 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 preserve permissions, ownership, and timestamp information during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option is used to compress the data during transfer, reducing the amount of data that needs to be transmitted over the network. Finally, the -e ssh option specifies the use of SSH encryption.

Using encryption with rsync is a useful feature that can help to ensure that sensitive data is transmitted securely over the network. When using SSH encryption, the data is encrypted before being transmitted over the network, making it difficult for attackers to intercept or read the data. However, it’s important to ensure that the systems involved in the transfer are properly secured and that appropriate security measures are in place to protect the data being transmitted.

Preserve Permissions and Ownership of Files:

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

To preserve permissions and ownership information of files during the 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 preserve permissions, ownership, and timestamp information during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option is used to compress the data during transfer, reducing the amount of data that needs to be transmitted over the network. Finally, the –perms and –owner options are used to preserve the permissions and ownership information of the files.

Using the –perms and –owner options can be essential when transferring files between systems with different user and group permissions, ensuring that the correct permissions and ownership information is retained. This can help to prevent issues with file access and security, ensuring that the transferred files can be used as intended on the destination system.

Transfer Files with Verbose Output:

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

To transfer files with verbose output using rsync, 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 preserve permissions, ownership, and timestamp information during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option is used to compress the data during transfer, reducing the amount of data that needs to be transmitted over the network.

Using the -v or –verbose option is a helpful feature that can help to ensure that the transfer is progressing as expected and that any errors or warnings are detected and resolved quickly. The verbose output can provide detailed information about the transfer, including the number of files transferred, the transfer rate, and any errors or warnings during the transfer. This information can be beneficial when transferring large amounts of data or working with slow or unreliable network connections.

Use Dry Run to Test Rsync Command:

Performing a dry run with the rsync command can be useful to test your command and ensure that you are transferring the correct files without overwriting any important data. A dry run allows you to simulate and preview what would be transferred without copying any files.

To use the dry run option with rsync, add the -n or –dry-run option to your command. For example, you can use the following command to perform a dry run of the rsync transfer:

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

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

Performing a dry run before the actual transfer can help to prevent accidental data loss or corruption by allowing you to check the transfer before it is executed. This can be particularly useful when working with large or critical files, where data loss could have significant consequences.

Transfer Files with Bandwidth Limit:

When transferring large files or directories over a network, it can be important to limit the bandwidth used by the transfer to avoid saturating the network and impacting other users. To limit the bandwidth used, you can use the –bwlimit option with the rsync command.

To transfer files with bandwidth limit using rsync, 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 preserve permissions, ownership, and timestamp information during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option is used to compress the data during transfer, reducing the amount of data that needs to be transmitted over the network. Finally, the –bwlimit option limits the bandwidth to 500 kilobits per second.

Limiting the bandwidth used by the rsync command can be particularly useful when working with a slow or congested network or when working with large files that could saturate the network and impact other users. By setting a bandwidth limit, you can ensure that the transfer does not negatively impact other network activities while still completing the transfer within a reasonable amount of time.

Transfer Files with Resume Option:

If a transfer is interrupted or stopped for any reason, you can use the rsync command with the –partial and –progress options to resume the transfer from where it left off.

To transfer files with the resume option using rsync, 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 preserve permissions, ownership, and timestamp information during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option is used to compress the data during transfer, reducing the amount of data that needs to be transmitted over the network. Finally, the –partial and –progress options are used to enable the resumption of an interrupted transfer and to display progress information during the transfer.

The –partial option instructs rsync to keep partially transferred files, while the –progress option displays the transfer progress, including the amount of data transferred, the transfer rate, and the estimated time remaining.

Using the resume option with rsync can be particularly useful when transferring large files over an unreliable network or when the transfer may be interrupted for any reason. By resuming the transfer from where it left off, you can avoid having to start it over again, saving time and bandwidth.

Transfer Files with IPv4 or IPv6 Protocol:

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

To transfer files with the IPv4 or IPv6 protocol using rsync, 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 preserve permissions, ownership, and timestamp information during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option is used to compress the data during transfer, 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 instead.

By default, rsync will try to use the most appropriate protocol available for network communication. However, specifying a particular protocol can be useful in cases where you want to ensure that the transfer uses a specific protocol or when the network configuration may be limiting the available protocols.

Transfer Files with Rsync over SSH:

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

For example, the following command transfers files over SSH using the default SSH command:

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

In this command, the -a or –archive option is used to preserve permissions, ownership, and timestamp information during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option is used to compress the data during transfer, 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 made over an SSH connection.

On the other hand, the following command uses the SSH command with the -l option to specify the username for 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 username for the SSH connection. This option is passed as an argument to the ssh command, specified with the -e option. By using -l user instead of user@remote, it specifies the username as an option to the SSH command, which can be useful in situations where the remote host does not allow SSH username specification in the user@host format.

Both commands aim to transfer files over an SSH connection with rsync. However, the second option specifies the SSH username more explicitly and can be useful in situations where the first option is impossible.

Transfer Files with Remote Shell:

When transferring files over a network, it’s sometimes necessary to use a remote shell to access the destination machine. 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 transfers files using the SSH remote shell with the specified username “user”:

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

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

This is useful when direct access to the destination machine is impossible or when using a remote shell provides 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 for this purpose. To create regular backups using rsync, you can use the -a or –archive option, which preserves the permissions, ownership, and timestamp of the files. This option ensures that the backup is an exact replica of the source directory, making it easier to restore the data in case of a failure or loss.

To perform a backup with 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 preserve the permissions, ownership, and timestamp information of the files during the transfer. The -v or –verbose option provides detailed output during the transfer, while the -z or –compress option compresses the data during transfer, reducing the amount of data that needs to be transmitted over the network.

This command would create a backup of the source directory in the backup directory, preserving the permissions, ownership, and timestamp of the files. It is important to note that the destination directory should be on a different device or location than the source directory to avoid losing both in case of a system failure.

Regular backups using rsync can help you ensure that important files and directories are safe and can be easily restored in a disaster.

Transfer Files with 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, increasing the transfer speed.

To transfer files with multithreading using rsync, 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 preserve the permissions, ownership, and timestamp information during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option is used to compress the data during transfer, 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 you to specify the number of parallel threads to use for the transfer. The optimal number of threads will depend on the available network bandwidth, the size of the files being transferred, and other factors. It is recommended to experiment with different values to find the optimal number for your specific situation.

Multithreading can greatly improve the speed of rsync transfers, particularly when transferring large files or directories over a 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 can increase the load on the network.

Transfer Files with Rsync Daemon:

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

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

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

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

The destination in this command is specified using the rsync:// protocol, followed by the hostname of the remote system and the destination directory. By default, the rsync daemon listens on TCP port 873, so you must ensure that this port is open on the remote system.

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

Transfer Files with Rsync Modules:

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

Transfer Files with Rsync Modules:

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

In this command, the rsync daemon is running on the remote system and is configured with a module called “module.” The contents of this module are transferred to the destination directory on the local system.

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

Using modules with rsync can be a powerful way to organize and transfer files between systems. By grouping files and directories into modules, you can simplify accessing and transferring specific data sets.

Use Rsync with Crontab:

Automating backups or file transfers is an essential 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 backups or transfers, use the crontab -e command to edit the crontab file and add the rsync command. The command must include the required options and arguments, such as the source directory, destination directory, and any other options needed for the transfer.

For example, the following command would 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, the crontab is set to run at 0 minutes past midnight every day. The rsync command uses the -a or –archive option to preserve permissions, ownership, and timestamp information and the -v or –verbose option to provide detailed output during the transfer. The -z or –compress option is used to compress the data during transfer, reducing the amount of data that needs 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, you can ensure that your important files are regularly backed up or transferred without requiring manual intervention.

Transfer Files with Batch Mode:

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

Here’s an example command that demonstrates how to use batch mode with rsync:

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

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

To create the filelist.txt file, you can use any text editor to create a list of files to be transferred, with one file per line. Once you have created the filelist.txt file, 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, you can transfer large numbers of files more efficiently and effectively without transferring unnecessary files or directories.

Transfer Files with Custom Logging:

To monitor the progress of rsync transfers and capture any errors or warnings, you can use the –log-file option to create a custom log file. This can be particularly helpful when troubleshooting issues or auditing transfer activity.

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

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

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

The rsync command will transfer the contents of the source directory to the destination directory, and all output will be logged to the rsync.log file. This log file can then be used to review the transfer activity, troubleshoot any errors or warnings that occurred, or audit the transfer history.

It is important to note 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 Dry Run and Custom Logging:

To simulate a rsync transfer without actually copying any files and keep track of the output in a custom log file, you can use the combination of the –dry-run and –log-file options with the rsync command.

The –dry-run option (or -n) allows you to preview the transfer, showing what files would be transferred and actions are taken without making any changes to the source or destination directories. This can be useful for testing and verifying the rsync command before transferring files.

The –log-file option (or -l) allows you to specify a custom log file to record the rsync output, which can be helpful for auditing and troubleshooting. This option writes a detailed log of the transfer, including any errors or warnings during the process.

For example, the following command performs a dry run of the rsync command and logs the output to a custom file named rsync.log:

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

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

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

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

Use Rsync to Compare Files and Directories:

Rsync’s ability to compare files and directories between the source and destination and only transfer the differences is a key feature of the tool. This capability helps to save time and bandwidth by transferring only the changes rather than copying the entire file or directory.

To compare files and directories between the source and destination with rsync, you can use the -n or –dry-run option. This option performs a dry run of the command without transferring files. Instead, it shows you what would be transferred between the source and destination.

The following command shows an example of how to use rsync to compare files and directories between the source and destination with the dry run option:

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

In this command, the -a or –archive option preserves the permissions, ownership, and timestamp information of the files during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -n or –dry-run option performs a dry run of the rsync command to show what would be transferred without copying any files.

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

Transfer Files with Partially Transferred Directory:

When transferring large files or directories with rsync, interruptions or failures can sometimes occur, resulting in only partial transfers. In such cases, you can use the –partial-dir option with the rsync command to specify a directory to store partially transferred files.

The example command for transferring files with partially transferred 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 preserve the permissions, ownership, and timestamp of the files during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option compresses the data during transfer, reducing the amount of data that needs to be transmitted over the network.

The –partial-dir option is used to specify the directory where partially transferred files will be stored. In this case, any partially transferred files will be stored in the .rsync-partial directory.

Using the –partial-dir option can help ensure that files are not lost due to interrupted or failed transfers and can also make it easier to resume transfers from where they left off.

Use Rsync to Copy Files to Multiple Locations:

Copying files to multiple locations can be a time-consuming task. However, rsync with the –files-from option can make the process more efficient. The –files-from option allows you to specify a list of files to transfer. Using the rsync command with multiple destination directories allows you to copy those files to several locations simultaneously.

The example command for copying files to multiple locations is as follows:

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

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

The –files-from option is used to specify a list of files to transfer, and the two destination directories are specified at the end of the command. The files listed in the filelist.txt file would be transferred to both the /path/to/destination1 and /path/to/destination2 directories on the remote system.

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

Use Rsync to Delete Files on Destination:

When transferring files with rsync, it is sometimes necessary to delete files on the destination that no longer exist on the source. To accomplish this, you can use the –delete option with the rsync command. This option tells rsync to remove any files on the destination that are not present on the source.

The command to transfer files and delete any files on the destination 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 preserve the permissions, ownership, and timestamp information of the files during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option compresses the data during transfer, reducing the amount of data that needs to be transmitted over the network.

The –delete option is used to delete files on the destination that no longer exist on the source. Using the –delete option can help keep the destination directory in sync with the source directory, ensuring that both directories have the same set of files.

Use Rsync to Sync Files between Two Servers:

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

The following command syncs 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 preserve the permissions, ownership, and timestamp information of the files during the transfer, while the -v or –verbose option provides detailed output during the transfer. The -z or –compress option compresses the data during transfer, 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. In contrast, the second remote path, user2@remote2:/path/to/destination, specifies the location of the destination directory on the second remote system.

When running this command, rsync will compare the files on both remote systems and transfer only the differences, ensuring that both systems have the same data.

Conclusion

In conclusion, the rsync command is a powerful tool for transferring files and directories between systems. With its ability to compare files and directories and only transfer the differences, rsync provides an efficient way to keep files in sync between systems or to perform regular backups. Additionally, the wide range of options and features provided by rsync allows for customization to suit specific needs, such as bandwidth limitation, transfer compression, encryption, and permissions preservation.

By mastering the various options and features of rsync, users can easily perform complex file transfers and ensure that data is transferred efficiently and reliably. From transferring files over SSH or the rsync daemon to syncing files between two servers, rsync is an invaluable tool for any system administrator, developer, or user who needs to move files between systems. With its speed, reliability, and versatility, rsync is a must-have tool for any Linux user.