How to Use Rsync on Linux to Synchronize Local and Remote Directories
Rsync is a powerful command-line tool for syncing files and directories between local and remote systems. It’s known for its speed and efficiency, making it ideal for backups, mirroring files, or simply keeping directories in sync.

What is Rsync?
Rsync stands for “remote sync” and is widely used for fast and incremental data transfers. Rsync minimizes data transfer by only copying differences between source and destination files, making it efficient and suitable for large data sets.
How to Use Rsync on Linux to Synchronize Local and Remote Directories – Step by Step Guide
Prerequisites
- A Linux system with `rsync` installed.
- SSH access to the remote server, if syncing remotely.
Basic Rsync Syntax
The general syntax for Rsync is:
rsync [options] source destination
Replace source and destination with the paths you want to sync. For remote synchronization, prefix the path with the remote server address, like user@remote_server:/path/to/directory.
Commonly Used Rsync Options
-a(Archive): Syncs recursively and preserves symbolic links, permissions, and timestamps.-v(Verbose): Provides detailed output on what Rsync is doing.-z(Compress): Compresses data during transfer for faster syncs.--delete: Deletes files from the destination if they don’t exist in the source.-P: Shows progress and partial data transfer details.
Example 1: Synchronize Local Directories
To sync the contents of a source directory with a target directory on the same system, run:
rsync -av /path/to/source/ /path/to/destination/
This command will copy any changes from the source to the destination directory.
Example 2: Synchronize with Remote Directories
To sync a local directory with a remote server directory:
rsync -av /path/to/local/ user@remote_server:/path/to/remote/
Make sure to replace user@remote_server with your server’s details.
Example 3: Mirror Directories with Deletion
To make the destination directory an exact mirror of the source, including deletions, use the --delete option:
rsync -av --delete /path/to/source/ /path/to/destination/
This removes any files in the destination that are no longer in the source directory.
Example 4: Sync Over SSH
Rsync can work over SSH for secure transfers. To sync files securely with a remote directory:
rsync -av -e "ssh -p 22" /path/to/local/ user@remote_server:/path/to/remote/
Replace -p 22 with your SSH port if it’s different from the default.
Example 5: Sync with Progress Display
Use the -P option to show the progress of large file transfers:
rsync -avP /path/to/source/ /path/to/destination/
The progress indicator helps you monitor transfer rates and remaining time.
Example 6: Exclude Specific Files or Directories
To exclude certain files or directories, use the --exclude option:
rsync -av --exclude 'file_or_directory_to_exclude' /path/to/source/ /path/to/destination/
This option allows you to skip specific files or directories during sync.
Scheduling Rsync Jobs with Cron
To automate Rsync syncs, schedule them with cron:
crontab -e
Add a cron job, for example, to run Rsync every day at midnight:
0 0 * * * rsync -av --delete /path/to/source/ /path/to/destination/
Adjust the schedule as needed to automate your backups or sync tasks.
With Rsync, you can efficiently sync local and remote directories, making it a valuable tool for backups, mirroring, and data management. By mastering the options and examples above, you’ll be able to tailor Rsync to meet a variety of data sync needs on Linux.
For further customization and advanced usage, consult the Rsync man page.
