rsync Command

What is Linux rsync Command?

rsync is a fast, versatile command-line utility for synchronizing files and directories between two locations. It can sync data locally on a single machine or between local and remote systems over SSH. The name “rsync” stands for “remote sync” and it’s designed to efficiently transfer and synchronize data by copying only the differences between source and destination. From the man page:

rsync is a file transfer program capable of efficient remote update via a fast differencing algorithm. It can copy files locally on a single machine, or copy/synchronize across the network to/from another host.

rsync uses a delta-transfer algorithm that sends only the differences between the source files and the existing files in the destination. This makes it extremely efficient for backing up and mirroring data, especially when dealing with large files or directories that change frequently.

Key Features

  • Incremental transfers: Only transfers changed parts of files
  • Preserve file attributes: Maintains permissions, timestamps, ownership, and links
  • Compression: Can compress data during transfer to save bandwidth
  • Resume capability: Can resume interrupted transfers
  • Flexible synchronization: Various options for handling deletions and updates
  • Cross-platform: Works on Linux, macOS, Windows, and other Unix-like systems

rsync Syntax

rsync [options] source destination

The basic syntax involves specifying options, followed by the source location and destination location.

Basic rsync Examples

Local File Synchronization

Copy files locally within the same system:

rsync -av /home/user/documents/ /backup/documents/

Basic Remote Synchronization

Sync files to a remote server:

rsync -av /local/directory/ user@remote-server:/remote/directory/

Sync files from a remote server:

rsync -av user@remote-server:/remote/directory/ /local/directory/

Essential rsync Options

Archive Mode (-a)

The -a option is the most commonly used flag, equivalent to -rlptgoD:

rsync -a source/ destination/

This preserves:

  • -r: Recursive (copy directories)
  • -l: Preserve symbolic links
  • -p: Preserve permissions
  • -t: Preserve modification times
  • -g: Preserve group ownership
  • -o: Preserve user ownership
  • -D: Preserve device files and special files

Verbose Output (-v)

Show detailed output of what rsync is doing:

rsync -av source/ destination/

Compression (-z)

Compress data during transfer (useful for remote sync):

rsync -avz source/ user@server:/destination/

Progress Display (–progress)

Show transfer progress:

rsync -av --progress source/ destination/

Local Synchronization Examples

Sync Two Local Directories

rsync -av /home/user/photos/ /backup/photos/

Sync with Deletion

Remove files in destination that don’t exist in source:

rsync -av --delete /home/user/documents/ /backup/documents/

Remote Synchronization Examples

Syncing over SSH

rsync -avzP /local/path/ [email protected]:/remote/path/
  • -P: Combines --partial (keep partially transferred files) and --progress (show progress).

Specifying a Different SSH Port

rsync -avz -e 'ssh -p 2222' /local/path/ [email protected]:/remote/path/

Using rsync Daemon

rsync -av rsync://user@host/module /local/path/

Advanced rsync Usage

Exclude Files and Directories

Exclude specific files or patterns:

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

Use an exclude file:

rsync -av --exclude-from='exclude-list.txt' source/ destination/

Include/Exclude Rules

Order matters: more specific rules should come before general ones.

rsync -av --include='*.jpg' --exclude='*.{tmp,bak}' source/ destination/

Bandwidth Limiting

Limit transfer speed to prevent network saturation:

rsync -av --bwlimit=100k source/ destination/

Dry Run

Test your rsync command without making any changes:

rsync -av --dry-run source/ destination/

rsync Command Manual / Help

We can use man and info command to see the manual page of rsync command. rsync command also have --help option to show list of options.

To open man page for rsync command we can use command below. To exit man or info page you can press q.

man rsync

To open info page for rsync command we can use command below.

info rsync

To open help page from rsync command we can run command below.

rsync --help

rsync Command Source Code

You can find rsync command source code from the following repositories:

You can read tutorials of related Linux commands below:

Summary

In this comprehensive tutorial, we’ve covered the essential aspects of using rsync for file synchronization and backup tasks. rsync is a powerful tool that efficiently handles both local and remote file transfers by copying only the differences between source and destination.

Key takeaways:

  • rsync uses delta-transfer algorithm for efficient synchronization
  • Archive mode (-a) preserves file attributes and is most commonly used
  • Remote synchronization works seamlessly over SSH
  • Advanced features include compression, bandwidth limiting, and selective sync
  • Perfect for automated backups, website deployment, and data migration

Visit our Linux Commands guide to learn more about using command line interface in Linux.