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 / Inspect, Pause, and Stop Processes

Linux Tutorial / Inspect, Pause, and Stop Processes

Inspect Linux processes with ps, top, and pgrep, then safely pause, resume, and terminate a practice process while distinguishing shell jobs from services.

Linux ln Command: Create Hard Links and Symbolic Links

Linux ln Command: Create Hard Links and Symbolic Links

Learn how to create hard links and symbolic links with Linux ln, choose relative or absolute targets, replace links safely, and inspect link behavior.

Linux sftp Command: Transfer Files Securely over SSH

Linux sftp Command: Transfer Files Securely over SSH

Learn how to use the Linux sftp command to transfer and manage files securely through an SSH connection, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux groups Command: Show a User's Group Memberships

Linux groups Command: Show a User's Group Memberships

Learn how to show a User's Group Memberships with the Linux groups command, including practical examples, key options, and important precautions.

Linux systemctl Command: Manage systemd Services

Linux systemctl Command: Manage systemd Services

Learn how to manage systemd Services with the Linux systemctl command, including practical examples, key options, and important precautions.

Linux Tutorial / Install and Update Packages with APT and DNF

Linux Tutorial / Install and Update Packages with APT and DNF

Compare APT on Debian or Ubuntu with DNF on Rocky Linux for searching, installing, updating, and removing packages, including repository and transaction checks.

Linux apt Command: Manage Packages on Debian and Ubuntu

Linux apt Command: Manage Packages on Debian and Ubuntu

Learn how to manage Packages on Debian and Ubuntu with the Linux apt command, including practical examples, key options, and important precautions.

Linux truncate Command: Shrink or Extend File Size

Linux truncate Command: Shrink or Extend File Size

Learn how to shrink or extend files with Linux truncate, adjust sizes relatively, match a reference file, and distinguish sparse logical size from disk usage.

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 pwd Command: Print the Current Working Directory

Linux pwd Command: Print the Current Working Directory

Learn how to use the Linux pwd command to print the logical or physical absolute path of the current working directory, with essential options, practical examples, output interpretation, and common troubleshooting tips.