Skip to content

rsync: Efficient File Synchronization and Transfer

Summary

rsync is a versatile command-line utility for efficiently synchronizing files and directories between two locations, either locally or over a network. It minimizes data transfer by only copying the differences between the source and destination.

Introduction

rsync stands for "remote sync." It's a powerful tool for backups, mirroring data, and copying files quickly and efficiently. Unlike simple copy commands, rsync uses an algorithm that identifies the differences between files and only transfers those changes, making it ideal for large datasets and slow network connections. It's available on most Unix-like systems and also has implementations for Windows.

Use case and Examples

Simple Local Copy

rsync -avz /path/to/source /path/to/destination
This command copies all files and directories from /path/to/source to /path/to/destination recursively, preserving permissions, ownership, and timestamps. The -a flag is a shorthand for several common options.

Remote Synchronization (Push)

rsync -avz /path/to/source user@remote_host:/path/to/destination
This command synchronizes the local directory /path/to/source with the remote directory /path/to/destination on the server remote_host as the user user.

Remote Synchronization (Pull)

rsync -avz user@remote_host:/path/to/source /path/to/destination
This command synchronizes the remote directory /path/to/source on the server remote_host as the user user with the local directory /path/to/destination.

Deleting Files at the Destination

rsync -avz --delete /path/to/source /path/to/destination
This command synchronizes /path/to/source with /path/to/destination, and also deletes any files in the destination directory that do not exist in the source directory. Use with caution!

Excluding Files and Directories

rsync -avz --exclude '*/tmp/*' --exclude '*.log' /path/to/source /path/to/destination
This command excludes any files or directories matching the patterns '*/tmp/*' and '*.log' from the synchronization process.

Commonly used flags

Flag Description Example
-a Archive mode; preserves permissions, ownership, timestamps, symlinks, etc. rsync -a /src /dest
-v Verbose; increases the amount of information displayed during the transfer. rsync -v /src /dest
-z Compress file data during the transfer. Useful for slower network connections. rsync -z /src /dest
--delete Deletes files in the destination that do not exist in the source. rsync --delete /src /dest
-n or --dry-run Performs a dry run; shows what would be transferred without actually doing it. rsync -n /src /dest
--exclude Excludes files or directories matching a specified pattern. rsync --exclude '*.txt' /src /dest
-P Shows progress and keeps partially transferred files if the transfer is interrupted. rsync -P /src /dest


Share on Share on

Comments