rsync Command in Linux



rsync command is a Linux command line utility used for transferring and synchronizing files and directories. This command is known for its speed and flexibility and is widely used by system administrators, developers, and anyone needing efficient backup, remote synchronization, or mirroring of data. It minimizes data transfer by only copying the differences between source and destination files, making it a preferred tool for network and local file operations.

Table of Contents

Here is a comprehensive guide to the options available with the rsync command −

Syntax of rsync Command

The basic syntax for the Linux rsync command is as follows −

rsync [options] source destination

Where −

  • [options] − These are optional flags that modify the behavior of the rsync command.
  • source − The path to the directory or files you want to transfer or synchronize.
  • destination − The path where the files will be copied to.

rsync Command Options

Here's a rundown of some notable options you can use with the command rsync

OptionDescription
--archive, -aEnables archive mode, which recursively copies files while preserving symbolic links, file permissions, user & group ownership, and timestamps.
--recursive, -rAllows rsync to traverse directories and transfer their contents recursively.
--checksum, -cUses checksum comparison instead of mod-time and size to decide if the files need to be synchronized.
--progressDisplays the progress of the file transfer, providing a real-time view of the remaining data to be synced.
--compress, -zCompresses file data during transfer to reduce transfer time, especially useful for slow network connections.
--deleteDeletes files in the destination directory that do not exist in the source directory, helping to maintain an exact mirror.
--links, -lEnsures that symlinks in the source are copied as symlinks in the destination.
--excludeExcludes files matching a specified pattern from being transferred.
--includeIncludes files matching a specified pattern, even if they are excluded by other filter rules.
--backup, -bMakes backups of existing files before transferring new versions. This option helps in preserving old copies.
--suffix=SUFFIXSpecifies a directory to store backup files, used in conjunction with --backup.
--times, -tPreserves modification times.
--perms, -pPreserves file permissions.

Examples of rsync Command in Linux

Here are some practical examples to demonstrate how to use rsync with the options discussed −

  • Using Archive Mode
  • Synchronising with Checksum
  • Compressing Data During Transfer
  • Deleting Extraneous Files
  • Copying Symlinks
  • Excluding Specific Files
  • Setting Backup Suffix

Using Archive Mode

If you need to perform a comprehensive backup, preserving all attributes like symbolic links, file permissions, and timestamps, you can use −

rsync -a source/ destination/

This command enables archive mode, ensuring that the entire directory structure and file attributes are preserved during the transfer.

rsync Command in Linux1

Synchronizing with Checksum

To ensure accurate synchronization by comparing checksums instead of modification times and file sizes, you can use −

rsync -c source/ destination/

This command uses checksum comparison to determine if files need to be synchronized, providing a higher level of accuracy.

rsync Command in Linux2

Compressing Data During Transfer

If you want to compress the file data during transfer to save bandwidth, especially useful for slow network connections, you can use −

rsync -avz source/ destination/

This command compresses the file data during the transfer, reducing transfer time and bandwidth usage.

rsync Command in Linux3

Deleting Extraneous Files

To maintain an exact mirror by deleting files in the destination directory that are not present in the source directory, you can use −

rsync -av --delete source/ destination/

This command deletes files in the destination that do not exist in the source, ensuring an exact mirror.

rsync Command in Linux4

Copying Symlinks

When you need to ensure that symlinks in the source are copied as symlinks in the destination, you can use −

rsync -av --links source/ destination/

This command ensures that symbolic links in the source directory are preserved in the destination directory.

rsync Command in Linux5

Excluding Specific Files

If you need to exclude files matching a specific pattern during the transfer, you can use −

rsync -av --exclude '*.tmp' source/ destination/

This command excludes files with the .tmp extension from being transferred, focusing only on the relevant files.

rsync Command in Linux6

Setting Backup Suffix

If you want to specify a suffix for backup files, you can use −

rsync -avb --suffix=.bak source/ destination/

This command backs up existing files with the .bak suffix before transferring new versions.

rsync Command in Linux7

Conclusion

The rsync command stands as a quintessential tool for file synchronization and transfer in Linux environments. Whether you're looking to back up data, mirror directories, or synchronize files across remote systems, rsync offers an array of options to tailor the process to your specific needs. Its efficiency lies in transferring only the changed portions of files, conserving bandwidth and time.

By mastering the syntax and options of rsync, users can streamline their data management tasks, ensuring reliable and up-to-date backups.