LinuxCommandLibrary

rsync

Synchronize files and directories

TLDR

Transfer a file

$ rsync [path/to/source] [path/to/destination]
copy

Use archive mode (recursively copy directories, copy symlinks without resolving, and preserve permissions, ownership and modification times)
$ rsync [[-a|--archive]] [path/to/source] [path/to/destination]
copy

Compress the data as it is sent to the destination, display verbose and human-readable progress, and keep partially transferred files if interrupted
$ rsync [[-zvhP|--compress --verbose --human-readable --partial --progress]] [path/to/source] [path/to/destination]
copy

Recursively copy directories
$ rsync [[-r|--recursive]] [path/to/source] [path/to/destination]
copy

Transfer directory contents, but not the directory itself
$ rsync [[-r|--recursive]] [path/to/source]/ [path/to/destination]
copy

Use archive mode, resolve symlinks, and skip files that are newer on the destination
$ rsync [[-auL|--archive --update --copy-links]] [path/to/source] [path/to/destination]
copy

Transfer a directory from a remote host running rsyncd and delete files on the destination that do not exist on the source
$ rsync [[-r|--recursive]] --delete rsync://[host]:[path/to/source] [path/to/destination]
copy

Transfer a file over SSH using a different port than the default (22) and show global progress
$ rsync [[-e|--rsh]] 'ssh -p [port]' --info=progress2 [host]:[path/to/source] [path/to/destination]
copy

SYNOPSIS

rsync [OPTION...] SRC... DEST
rsync [OPTION...] SRC... [USER@]HOST:DEST
rsync [OPTION...] [USER@]HOST:SRC... [DEST]
rsync [OPTION...] [USER@]HOST::SRC... [DEST]

PARAMETERS

-a, --archive
    Archive mode; a quick way to sync files while preserving permissions, ownership, timestamps, and symbolic links. Equivalent to -rlptgoD.

-v, --verbose
    Increase verbosity, providing more information about the transfer process.

-z, --compress
    Compress file data during the transfer, useful for slow network connections.

-h, --human-readable
    Output numbers in a human-readable format, such as file sizes.

-n, --dry-run
    Perform a trial run without making any actual changes, useful for testing commands.

-r, --recursive
    Recurse into directories, copying their contents. Implied by -a.

--delete
    Delete extraneous files from the destination directory that are not present in the source.

-P
    A combination of --partial (keep partially transferred files) and --progress (show transfer progress).

--exclude=PATTERN
    Exclude files or directories matching the specified PATTERN from the transfer.

--include=PATTERN
    Don't exclude files or directories matching the specified PATTERN, even if excluded by a previous --exclude rule.

-t, --times
    Preserve modification times of files.

-p, --perms
    Preserve permissions of files.

-g, --group
    Preserve group ownership of files.

-o, --owner
    Preserve owner (user) ownership of files (requires super-user privileges).

--bwlimit=KBPS
    Limit the network bandwidth used by rsync, specified in kilobytes per second.

DESCRIPTION

rsync is a powerful and versatile command-line utility for synchronizing files and directories between two locations, whether on the same machine or across a network. Its primary strength lies in its delta-transfer algorithm, which detects and transfers only the parts of files that have changed since the last synchronization, significantly reducing data transfer and bandwidth usage. This makes rsync exceptionally efficient for incremental backups, mirroring, and maintaining up-to-date copies of data. Beyond simple copying, rsync can preserve a wide array of file attributes, including permissions, ownership, modification times, symbolic links, hard links, and even extended attributes. It supports various modes of operation, including local file copying, remote synchronization via SSH, and direct connections to an rsync daemon. Its flexibility and efficiency make it an indispensable tool for system administrators and users alike.

CAVEATS

rsync is a powerful tool that requires careful usage, especially concerning its deletion features. Always use --dry-run (-n) first when combining with --delete to prevent unintended data loss. The behavior of rsync can be drastically different based on whether the source path has a trailing slash (e.g., src/ vs. src). A trailing slash means "copy the contents of this directory," while no trailing slash means "copy the directory itself." When using rsync for remote operations, ensure proper SSH key setup or passwordless authentication for automation, or secure the rsync daemon if used. Preserving ownership (-o) or special files (-D) often requires super-user privileges on the destination system.

TRAILING SLASH SEMANTICS

The presence or absence of a trailing slash on the source path fundamentally changes rsync's behavior. For example, rsync -a src/ dest copies the contents of src into dest. In contrast, rsync -a src dest copies the directory src itself (and its contents) into dest, resulting in dest/src. This distinction is crucial for correct synchronization and mirroring operations.

DELTA-TRANSFER ALGORITHM

At its heart, rsync uses a sophisticated algorithm to minimize data transfer. It breaks files into blocks and computes checksums for these blocks. When synchronizing, it compares the checksums of the source file blocks with those of the destination file. Only blocks that have changed or are missing on the destination are transferred, making it extremely efficient for updating large files with small modifications. This significantly reduces network bandwidth usage and transfer time compared to simply copying entire files.

HISTORY

rsync was originally authored by Andrew Tridgell and Paul Mackerras, with its first public release in June 1996. Its development was a direct response to the need for a more efficient file transfer utility, particularly over slow or unreliable networks. The core innovation behind rsync's efficiency is its delta-transfer algorithm, which intelligently detects and transfers only the differences between files, rather than copying entire files. This groundbreaking approach quickly established rsync as an indispensable tool for backups, mirroring, and general system administration, significantly influencing the design of subsequent data synchronization protocols.

SEE ALSO

cp(1), scp(1), sftp(1), tar(1), ssh(1)

Copied to clipboard