Copying files with cp or scp is fine until you’re dealing with directories of thousands of files, slow connections, or incremental backups. rsync solves all of that in one command.
What Is rsync and How Does It Work?
rsync is a file synchronization and transfer tool for Linux. Unlike cp, which copies everything every time, rsync analyzes source and destination and transfers only what has changed. This makes it dramatically faster for repeated transfers.
The Delta-Transfer Algorithm
rsync breaks files into fixed-size chunks and computes checksums for each. It compares checksums between source and destination, then transfers only the chunks that differ. On a 1GB file where you changed 10KB, rsync transfers roughly 10KB. scp transfers 1GB. The difference matters on slow links or large backup sets.
Installing rsync
On Debian/Ubuntu:
sudo apt install rsyncOn RHEL/CentOS:
sudo yum install rsyncVerify your version:
rsync --versionrsync Syntax and Key Options
Basic syntax:
rsync [OPTIONS] SOURCE DESTINATIONCore Option Flags Explained
The flags you’ll use on every rsync command:
- -a (–archive): Archive mode. Recursively copies directories and preserves permissions, timestamps, symbolic links, and device files. Use this almost always.
- -v (–verbose): Shows each file being transferred. Add -vv for even more detail.
- -z (–compress): Compresses data during transfer. Saves bandwidth on slow connections, adds CPU overhead on fast ones.
- -P: Combines –progress and –partial. Shows transfer progress and keeps partial files if interrupted.
- –delete: Deletes files in the destination that no longer exist in the source. Keeps the destination as an exact mirror.
Using -n for Dry Run Testing
The rsync dry run flag (-n or –dry-run) simulates the transfer without actually moving anything:
rsync -avn source/ destination/Always run a dry run first when using –delete. It shows exactly which files would be deleted before you commit to it.
Monitoring Progress with –progress
For large transfers, –progress shows per-file transfer speed and estimated completion:
rsync -av --progress source/ destination/The rsync progress output shows bytes transferred, transfer rate, and estimated time remaining.
How to Use rsync: Practical Examples
Sync Files Locally
Copy a local directory to another location:
rsync -av /home/user/documents/ /backup/documents/Note the trailing slash on the source. With a trailing slash, rsync copies the contents of the directory. Without it, rsync copies the directory itself into the destination.
Copy Files to a Remote Server via SSH
rsync over SSH uses the same syntax as scp but with rsync’s efficiency:
rsync -avz -e ssh /local/path/ user@remote-server:/remote/path/The -e ssh flag tells rsync to use SSH for the transport layer. Add a custom port if needed:
rsync -avz -e "ssh -p 2222" /local/path/ user@remote:/remote/path/Sync from Remote Server to Local
Pull files from a remote server:
rsync -avz user@remote-server:/remote/path/ /local/destination/Same syntax, just source and destination reversed.
Backup a Directory with rsync
A complete rsync backup command that mirrors a source directory and removes deleted files:
rsync -av --delete /source/directory/ /backup/directory/Run this in a cron job for automated incremental backups.
Filtering: Exclude and Include Patterns
Excluding Files and Directories
The rsync exclude option keeps specific files or directories out of the transfer:
rsync -av --exclude='*.log' source/ destination/Exclude multiple patterns:
rsync -av --exclude='*.log' --exclude='tmp/' source/ destination/For complex filter rules, use a file:
rsync -av --exclude-from='exclude-list.txt' source/ destination/Using –delete to Mirror Directories
The rsync delete option removes files from the destination that no longer exist in the source:
rsync -av --delete source/ destination/This turns rsync into a true mirroring tool. Be careful: files deleted from source will be deleted from destination too. Always dry run first.
Automating rsync with Cron
Run a nightly backup at 2 AM:
0 2 * * * rsync -az --delete /data/ /backup/data/ >> /var/log/rsync-backup.log 2>&1Redirect both stdout and stderr to a log file so you can check if the backup ran cleanly.
FAQ: rsync Command
It runs rsync in archive mode (-a, which preserves permissions and recurses directories), verbose mode (-v, shows files being transferred), and compressed mode (-z, compresses data in transit). It’s the standard starting point for most rsync commands.
Use: rsync -avz -e ssh /local/path/ user@server:/remote/path/ The -e ssh flag routes the transfer through an SSH connection. For non-standard ports: rsync -avz -e “ssh -p 2222” source/ user@host:/dest/
Add the -n flag: rsync -avn source/ destination/ This shows everything rsync would do without actually transferring or deleting anything. Essential before using –delete.
Use –exclude=’pattern’: rsync -av –exclude=’*.tmp’ source/ destination/ For multiple exclusions, add multiple –exclude flags or use –exclude-from with a file containing one pattern per line.