Linux rsync Command: Synchronize Files and Directories

rsync copies files and efficiently synchronizes changes between local or remote locations. It is useful for repeated transfers of large amounts of data, but if the destination path and delete options are incorrectly specified, data can be lost.

What is the rsync command?

Basically, it selects the transfer targets by comparing size and modification times. -a is archive mode, which combines recursive copying and preservation of key metadata. source/ copies the contents of a directory, while source copies the directory itself to the destination; this difference is important. For SSH-style remote paths, use user@host:path.

Basic syntax

rsync [options] source destination

The installed implementation and options may vary depending on the distribution. Check the description for your current system with man rsync.

Examples

Preview before actual changes

Check the destination path and copy range with the output list.

rsync -av --dry-run project/ backup/project/

Synchronizing local directories

Copies the contents inside the source directory to the destination.

rsync -av project/ backup/project/

Remote transfer via SSH

rsync must also be installed on the remote, and change the account/path to the actual values.

rsync -av project/ alice@server.example.com:/home/alice/project/

Preview before actual copy

rsync --dry-run shows the list of files scheduled to be changed but does not actually copy the data. The directory structure created in the destination varies depending on whether there is a trailing slash.

rsync -av --dry-run source/ backup/

Example output when source/ contains notes.txt but the destination does not:

sending incremental file list
notes.txt

This command means that the contents inside the source directory, not the directory itself, will be synchronized to backup/. If you add --delete, files that exist only in the destination may be deleted, so be sure to review the --dry-run results. After actual execution, verify important files by file count, size, or checksum.

-a combines recursive copying with preservation of several metadata fields, but it is not a complete backup of every ACL, extended attribute, or hard link. Choose additional options based on the required metadata, destination filesystem, and permissions. For important data, test restoring from the destination too.

Main Options and Format

Options/Format Description
-a This is archive mode, which combines recursive copy and preservation of main attributes.
-v Shows detailed items being processed.
-n, --dry-run Prints the work plan without making actual changes.
--delete Deletes destination files that are not in the source. Review in advance is required.
--exclude=pattern Excludes files matching the specified pattern from the transfer.
-e 'ssh -p port' Specifies the remote shell command and port.

Precautions when using

--delete actually removes the target files. Check the trailing slashes on the source and destination paths and the filter rules, and review the deletion list with --dry-run. rsync is not a backup solution itself, and mistakes or corruption in the source can also be replicated. Remote transfers usually require rsync on both sides, and archive mode does not preserve ACLs or extended attributes completely.

Frequently Asked Questions

What is the difference between source and source/ in rsync?

source/ copies the contents of that directory, while source copies the directory itself under the destination.

Official Documentation

You can check the exact behavior of options and implementation differences in the official rsync documentation.

More in This Category
Linux Tutorial / File System Basics: Paths and Key Directories

Linux Tutorial / File System Basics: Paths and Key Directories

Understand the Linux directory tree, read absolute and relative paths, and learn what common directories such as /home, /etc, and /var are used for.

Linux nslookup Command: Query DNS Names and Records

Linux nslookup Command: Query DNS Names and Records

Learn how to query DNS Names and Records with the Linux nslookup command, including practical examples, key options, and important precautions.

Linux tac Command: Print Lines in Reverse Order

Linux tac Command: Print Lines in Reverse Order

Learn how Linux tac prints records from last to first, handles multiple files and custom separators, and helps inspect recent log entries in reverse order.

Linux fmt Command: Reformat Plain-Text Paragraphs

Linux fmt Command: Reformat Plain-Text Paragraphs

Learn how to reformat Plain-Text Paragraphs with the Linux fmt command, including practical examples, key options, and important precautions.

Linux scp Command: Copy Files over SSH

Linux scp Command: Copy Files over SSH

Learn how to copy Files over SSH with the Linux scp command, including practical examples, key options, and important precautions.

Linux realpath Command: Resolve Absolute and Canonical Paths

Linux realpath Command: Resolve Absolute and Canonical Paths

Learn how to use the Linux realpath command to resolve relative paths, symbolic links, and redundant path components, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Compare Files with diff and cmp

Linux Tutorial / Compare Files with diff and cmp

Use diff and cmp to compare two files, read unified diff output, and distinguish a detected difference from an actual command error.

Linux machine-id: How to Check and Reset It for Cloned Systems

Linux machine-id: How to Check and Reset It for Cloned Systems

Learn what the Linux machine-id identifies, how to check it, and how to prepare cloned system images for unique IDs. Understand the difference between an empty file and first-boot initialization.

Linux zypper Command: Manage Packages on openSUSE and SUSE

Linux zypper Command: Manage Packages on openSUSE and SUSE

Learn how to manage Packages on openSUSE and SUSE with the Linux zypper command, including practical examples, key options, and important precautions.

Linux chgrp Command: Change a File's Group

Linux chgrp Command: Change a File's Group

Learn how to change a File's Group with the Linux chgrp command, including practical examples, key options, and important precautions.