Home > File & Directory Management > rsync

rsync: Synchronizing Files and Directories

rsync is a powerful utility for efficiently synchronizing files and directories between local or remote systems. It uses a delta-transfer algorithm that transfers only the changed parts, saving network bandwidth and optimizing transfer speeds. It is used for various purposes such as backups, mirroring, and file distribution.

Overview

rsync is highly efficient for synchronizing large files or a large number of files by using a 'delta-transfer' method that transfers only the changed parts of files. It can transfer files between local file systems, from local to remote, and from remote to local.

Key Features

  • Delta transfer (transfers only changed parts)
  • Supports local and remote synchronization
  • Preserves metadata such as permissions, ownership, and timestamps
  • Compressed transfer and bandwidth control
  • Specifies file exclusion/inclusion patterns

Common Options

rsync offers a wide range of options, but here are some of the most commonly used ones.

Basic Operations and Archive

Transfer Control

Network

Generated command:

Try combining the commands.

Description:

`rsync` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Examples demonstrating various usage scenarios of rsync.

Synchronize Local Directories (Most Common)

rsync -avz /path/to/source/ /path/to/destination/

Copies/synchronizes the contents of the source directory to the destination directory. The combination of -a (archive), -v (verbose), and -z (compress) options is most frequently used.

Push Files to a Remote Server

rsync -avz /path/to/local_source/ user@remote_host:/path/to/remote_destination/

Synchronizes the local source directory to the destination directory on a remote server.

Pull Files from a Remote Server

rsync -avz user@remote_host:/path/to/remote_source/ /path/to/local_destination/

Synchronizes the source directory on a remote server to the destination directory locally.

Mirroring Source and Destination (--delete Option)

rsync -avz --delete /path/to/source/ /path/to/destination/

Ensures the source directory and destination are exactly identical. Files not present in the source will be deleted from the destination. It is strongly recommended to test with `--dry-run` first.

Exclude Specific Files/Directories

rsync -avz --exclude='*.log' --exclude='temp/' /path/to/source/ /path/to/destination/

Excludes log files and the 'temp' directory from synchronization.

Simulate Before Execution (--dry-run)

rsync -avz --delete --dry-run /path/to/source/ /path/to/destination/

Shows what rsync will do without actually changing files. This is essential, especially before using options like `--delete`.

Installation

rsync is installed by default on most Linux distributions. If it is not installed, you can install it using the following commands.

Debian/Ubuntu

sudo apt update
sudo apt install rsync

CentOS/RHEL/Fedora

sudo yum install rsync

Arch Linux

sudo pacman -S rsync

Tips & Precautions

Tips and precautions for using rsync more effectively and safely.

Importance of the Trailing Slash (/) in Paths

The behavior changes depending on whether a trailing slash is present in the source path.

  • `/path/to/source/` (with slash): Copies the 'contents' of the `source` directory to the destination.
  • `/path/to/source` (without slash): Copies the `source` directory 'itself' to the destination. (The `source` directory will be created inside the destination directory)

Utilizing the --dry-run Option

Especially when using destructive options like `--delete`, always test with `--dry-run` first to prevent unexpected data loss.

Permissions and Ownership Issues

The `--archive` option attempts to preserve permissions and ownership, but issues may arise if the corresponding user/group does not exist on the destination system or if permissions are insufficient. In such cases, consider using `sudo` or options like `--no-p` (do not preserve permissions), `--no-o` (do not preserve owner), `--no-g` (do not preserve group).

Bandwidth Limiting

To avoid excessive network bandwidth usage, you can limit the transfer speed using the `--bwlimit=KBPS` option. (e.g., `--bwlimit=1000` limits to 1MB/s)


Same category commands