Linux Commands GuideLinux System AdministrationLinux Tutorials

Rsync Command in Linux: Complete Guide with Examples, Options, and Best Practices

The Rsync command in Linux is a powerful, flexible tool for fast file synchronization and transfer, designed to move only the changes between source and destination. Whether you manage backups, mirror directories, or migrate data across servers, rsync minimizes bandwidth and ensures consistent replicas. This guide covers installation, core syntax, practical examples for local and remote transfers, excluding files, mirroring with deletion, performance tuning, and troubleshooting tips. Examples include realistic command output and explanations so you can run rsync confidently in production environments. If you administer Debian, Ubuntu, RHEL, CentOS, Fedora, or Arch systems, these clear examples and best practices will help you integrate rsync into everyday workflows and automated backup scripts.

Installing Rsync on Popular Distributions

Rsync is included in most distribution repositories. Use your package manager to install or update it. Below are examples for Debian/Ubuntu and Fedora/RHEL-family systems. The sample output reflects typical package manager behavior after a successful install.

sudo apt update && sudo apt install rsync

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  rsync
0 upgraded, 1 newly installed, 0 to remove and 5 not upgraded.
Need to get 296 kB of archives.
After this operation, 1,024 kB of additional disk space will be used.

The sudo apt install rsync command installs rsync on Debian and Ubuntu. The output shows package manager activity: updating lists, resolving dependencies, download size, and post-install disk usage.

sudo dnf install rsync

Last metadata expiration check: 0:01:22 ago on Thu 04 Mar 2026 10:00:00 AM UTC.
Dependencies resolved.
================================================================================
 Package           Arch           Version                   Repository    Size
================================================================================
Installing:
 rsync             x86_64         3.2.7-1.fc34              fedora       285 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 285 k
Installed size: 1.0 M
Is this ok [y/N]: y

On Fedora, RHEL, and related systems use dnf. The example shows dependency resolution and a prompt. For minimal servers, run with -y to auto-accept.

Rsync Command Syntax and Common Options

The basic rsync syntax is simple: rsync [OPTIONS] SOURCE DEST. Common options are -a (archive), -v (verbose), -z (compress), -P (progress + partial), --delete (mirror deletions), and --exclude/ --exclude-from (skip files). Below is an example copying a single file locally with the archive and verbose flags so you can see rsync’s output and transfer statistics.

rsync -a -v /opt/filename.zip /tmp/

sending incremental file list
filename.zip
          1,048,576 100%    1.00MB/s    0:01:00 (xfer#1, to-check=0/1)

sent 1,050,000 bytes  received 42 bytes  2,100,084.00 bytes/sec
total size is 1,048,576  speedup is 1.00

In this example, -a preserves permissions, timestamps, ownership, and symlinks; -v prints progress messages. The output shows the file name, transfer progress, bytes sent/received, and speedup ratio.

Directory Synchronization and the Trailing Slash

Understanding the trailing slash on source directories is essential. A trailing slash copies the contents of the directory; without it, rsync copies the directory itself as a subdirectory of the destination. The example demonstrates syncing a web site directory into a backup directory while preserving contents.

rsync -a /var/www/domain.com/public_html/ /var/www/domain.com/public_html_backup/

sending incremental file list
index.html
css/style.css
images/logo.png

sent 12,345 bytes  received 78 bytes  24,846.00 bytes/sec
total size is 12,300  speedup is 0.99

Here the trailing slash in the source causes rsync to copy the contents of public_html directly into public_html_backup/. If the trailing slash were omitted, the result would be public_html_backup/public_html/....

Remote Synchronization over SSH (Common Use Cases)

The default remote transport for rsync is SSH. Rsync must be installed on both ends. Use passwordless SSH keys for automation. Below is a remote-to-local example using archive mode and progress reporting for clarity.

rsync -a -P remote_user@remote.example.com:/opt/media/ /opt/media/

sending incremental file list
file1.mp4
file2.mp4
          512,000 100%  10.00MB/s    0:00:05 (xfer#1, to-check=1/2)

sent 4,200,000 bytes  received 198 bytes  8,400,396.00 bytes/sec
total size is 5,000,000  speedup is 1.19

The -P option is shorthand for –partial –progress and is useful for large files and unstable links. It shows per-file progress and keeps partially transferred files so they can resume.

rsync -a -e "ssh -p 2322" /opt/media/ remote_user@remote.example.com:/opt/media/

sending incremental file list
videos/movie.mkv
          2,097,152 100%    5.00MB/s    0:07:00 (xfer#1, to-check=0/1)

sent 2,100,000 bytes  received 120 bytes  4,200,240.00 bytes/sec
total size is 2,097,152  speedup is 1.00

Use the -e option to pass a custom remote shell. In this example the SSH connection uses port 2322. This is required when the remote SSH server listens on a non-standard port.

Excluding Files and Exclude Files List

Exclude patterns avoid syncing temporary files or large directories like node_modules. Patterns are relative to the source path. For many exclusions use --exclude-from and keep a clean list file.

rsync -a --exclude=node_modules --exclude=tmp /src_directory/ /dst_directory/

sending incremental file list
app.js
package.json
dist/bundle.js

sent 253,400 bytes  received 64 bytes  506,928.00 bytes/sec
total size is 251,200  speedup is 0.99

Each --exclude pattern skips matching files or directories. The example excludes node_modules and tmp from the transfer.

rsync -a --exclude-from='/root/rsync-exclude.txt' /src_directory/ /dst_directory/

sending incremental file list
app.js
dist/bundle.js

sent 180,200 bytes  received 52 bytes  360,504.00 bytes/sec
total size is 178,904  speedup is 0.99

The –exclude-from option reads patterns from a file. Example /root/rsync-exclude.txt can contain lines like node_modules and *.log. This keeps command-line calls tidy and consistent.

Mirroring with –delete and Safe Operations

To mirror a source to a destination you often want to remove files at the destination that no longer exist at the source. Use --delete with care. Always test with --dry-run before destructive operations.

rsync -a --delete --dry-run /var/www/site/ /backup/site/

sending incremental file list
deleted old-file.html
index.html
css/style.css

sent 98 bytes  received 45 bytes  286.00 bytes/sec
total size is 10,240  speedup is 74.12

The –dry-run option simulates the operation and prints what would be changed or deleted. In this output old-file.html is marked as deleted; when satisfied, remove --dry-run to apply changes.

Permissions, Ownership, and Running as Root

Preserving ownership requires appropriate privileges. Use -a for ownership and permissions, but when copying between systems you may need to run rsync as root or use sudo on one or both sides. If you only need file data and metadata is irrelevant, avoid preserving ownership to prevent permission issues on shared systems.

Performance Tips and Troubleshooting

For large datasets, consider these optimizations: use –compress-level or avoid -z on fast LANs, run rsync with -P to resume interrupted transfers, increase --bwlimit for controlled bandwidth, and parallelize large independent transfers with GNU Parallel or background jobs. If rsync is slow, check CPU, disk I/O, and network latency. Use -vv for verbose debugging and --stats for detailed statistics.

Troubleshooting Common Errors

Permission denied: verify read access on source and write access on destination, and SSH key configuration for remote hosts. Connection refused: confirm SSH is listening and firewall rules permit it. Protocol mismatch: update rsync on both ends. Use –ignore-existing or rerun transfers when source files change during synchronization.

Conclusion

Rsync is a cornerstone utility for Linux administrators — efficient, scriptable, and adaptable to many transfer scenarios. Use -a for most archive needs, rely on -P for large files and unreliable links, and always test destructive flags like --delete with --dry-run first. Combine rsync with cron, systemd timers, or backup orchestration tools to build reliable, repeatable backups and replication across local disks and remote servers. With the examples and explanations here, you can apply rsync confidently to backups, migrations, and continuous synchronization jobs.

Komentariši

Vaša email adresa neće biti objavljivana. Neophodna polja su označena sa *