BLOG POSTS
    MangoHost Blog / Rsync: Practical Command Examples for Efficient File Synchronization
Rsync: Practical Command Examples for Efficient File Synchronization

Rsync: Practical Command Examples for Efficient File Synchronization

The rsync 🗃️ package in Linux is a widely used utility for fast and efficient file copying and synchronization, both locally and over a network. Its primary purpose is to synchronize files and directories from one location to another while minimizing data transfer by using delta encoding when appropriate.

Here are some key features and uses of rsync:

➡️ Efficient Data Transfer: rsync only transfers the changes made to files, rather than transferring entire files every time. This efficiency is particularly beneficial over slower network connections or for large datasets.
👉 Support for Local and Remote Files: It can synchronize files and directories between local machines or over a network using SSH or rsync daemon.
➡️ Bandwidth Throttling: Users have the option to limit the bandwidth used by rsync, making it less intrusive on network resources.
👉 Preservation of File Attributes: rsync can preserve various file attributes like permissions, ownership, timestamps, and symbolic links during the transfer.
👉 Error Checking and Recovery: It includes error checking algorithms and can resume transfers after an interruption, ensuring reliable file synchronization.
➡️ Versatility: rsync is commonly used for backups and mirroring, and it is a popular choice for deploying updates to servers or for maintaining backup servers.
👉 Scripting and Automation: Its command-line nature makes it easy to incorporate into scripts, allowing for automated backup and synchronization tasks.
➡️ Exclusion and Inclusion of Files: Users can specify patterns to exclude certain files or directories from synchronization or to include only specific types of files.

Installing Rsync on Ubuntu and CentOS

First, we need to install it.

Installing Rsync on Ubuntu:
1. Open Terminal.
2. Update the package list:
sudo apt-get update
3. Install Rsync:
sudo apt-get install rsync

Installing Rsync on CentOS:
1. Open Terminal.
2. Rsync is usually installed by default on CentOS. To check, run:
rsync --version
3. If it’s not installed, install Rsync using YUM:
sudo yum install rsync

Common Rsync 📁 Command Examples

Basic File Transfer:
rsync source_file user@destination_host:destination_folder
Transfers a single file to a remote host.

Basic Directory Transfer:
rsync -r source_directory user@destination_host:destination_folder
Recursively transfers a directory to a remote host.

Sync Directory to Remote with Deletion:
rsync -a --delete source_directory user@destination_host:destination_folder
Synchronizes a directory to a remote host and deletes files in the destination that are not in the source directory.

Sync with Progress Output:
rsync -avh --progress source_directory destination_directory
Transfers files with verbose and human-readable output, including progress for each file.

Transfer Using SSH:
rsync -avz -e ssh source_directory user@destination_host:destination_folder
Uses SSH for data transfer, adding encryption and security.

Exclude Files/Directories:
rsync -av --exclude 'pattern' source_directory destination_directory
Excludes files or directories matching a pattern.

Include Specific Files:
rsync -av --include 'pattern' --exclude '*' source_directory destination_directory
Includes only files or directories matching a specific pattern.

Transfer Files with a Specific Extension:
rsync -av --include '*/' --include '*.ext' --exclude '*' source_directory destination_directory
Transfers only files with a specific extension.

Bandwidth Limiting:
rsync -avz --bwlimit=1000 source_directory destination_directory
Limits the bandwidth used by rsync to 1000 KBytes per second.

Partial Transfer and Resume:
rsync -avP source_directory destination_directory
Supports partial transfer of files and allows resuming interrupted transfers.

Backup Mode:
rsync -av --backup --backup-dir=backup_directory source_directory destination_directory
Makes backups of files that are changed or deleted during the transfer.

Advanced Rsync Command Examples

Preserve Permissions:
rsync -avz --perms source_directory destination_directory
Preserves file permissions during the transfer.

Preserve Symlinks:
rsync -avz --links source_directory destination_directory
Transfers and preserves symbolic links.

Preserve All File Attributes:
rsync -avzP source_directory destination_directory
Preserves all file attributes (permissions, links, modification times, etc.) and shows progress.

Synchronize Only Newer Files:
rsync -avzu source_directory destination_directory
Synchronizes only newer files from the source to the destination.

Copy Files with a Size Limit:
rsync -avz --max-size='size' source_directory destination_directory
Transfers files that are below a certain size.

Verbose Output with Itemized Changes:
rsync -avzi source_directory destination_directory
Provides a verbose output with an itemized list of changes.

Quiet Mode:
rsync -avzq source_directory destination_directory
Runs rsync in quiet mode, suppressing non-error messages.

Compare Checksums:
rsync -avzc source_directory destination_directory
Skips based on checksums, not mod-time & size.

Transfer Without Recursion:
rsync -avz --no-R source_directory destination_directory
Transfers files without recursion into subdirectories.

Using Different Port for SSH:
rsync -avz -e 'ssh -p port_number' source_directory user@destination_host:destination_folder
Specifies a different port for SSH connections.

Dry Run:
rsync -avzn source_directory destination_directory
Performs a trial run with no changes made.

Transfer Files from a Remote Source to Local Destination:
rsync -avz user@source_host:source_directory destination_directory
Pulls files from a remote source to a local destination.

Backup with Timestamped Directory:
rsync -avz --backup --backup-dir=backup_`date +%Y%m%d` source_directory destination_directory
Creates a backup in a timestamped directory.

Hope these examples help you to create your 🙂


This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked