The Rsync command in Linux is one of the most powerful and efficient tools for synchronizing files and directories. It is widely used by system administrators and developers for backups, data migration, mirroring servers, and remote file transfers.
What makes Rsync special is its incremental transfer mechanism, which copies only the changed parts of files instead of transferring everything again. This saves bandwidth, time, and system resources.
You can use Rsync for:
- Mirroring data
- Incremental backups
- Copying files between servers
- Replacing scp, sftp, and even cp in many use cases
This guide walks you through installing Rsync, understanding its syntax, and using the most common options with practical examples.
Installing Rsync
Rsync is pre-installed on most Linux distributions and macOS. If it’s missing, install it using your package manager.
Install Rsync on Ubuntu and Debian
sudo apt install rsync
Install Rsync on CentOS and Fedora
sudo yum install rsync
Verify installation:
rsync --version
Rsync Command Syntax
Before using Rsync, it’s important to understand its basic syntax.
# Local to Local rsync [OPTION]... [SRC]... DEST # Local to Remote rsync [OPTION]... [SRC]... [USER@]HOST:DEST # Remote to Local rsync [OPTION]... [USER@]HOST:SRC... DEST
Parameters Explained
- OPTION – Rsync options that control behavior
- SRC – Source file or directory
- DEST – Destination path
- USER – Remote username
- HOST – Remote hostname or IP address
Commonly Used Rsync Options
Here are the most frequently used Rsync options:
-a, --archive
Archive mode (equivalent to -rlptgoD). It preserves permissions, ownership, timestamps, symbolic links, and recursively syncs directories.
-z, --compress
Compresses data during transfer. Useful for slow network connections.
-P
Equivalent to –partial –progress. Shows progress and keeps partially transferred files.
--delete
Deletes files from the destination that no longer exist in the source. Ideal for mirroring.
-q, --quiet
Suppresses non-error messages.
-e
Specifies a remote shell (default is SSH).
Basic Rsync Usage
Copy a File Locally
rsync -a /opt/filename.zip /tmp/
The user must have:
- Read permission on the source
- Write permission on the destination
Rename the File While Copying
rsync -a /opt/filename.zip /tmp/newfilename.zip
Synchronizing Directories
Rsync truly shines when syncing directories.
Create a Backup of Website Files
rsync -a /var/www/domain.com/public_html/ \ /var/www/domain.com/public_html_backup/
If the destination directory does not exist, Rsync creates it automatically.
Trailing Slash (/) Behavior
Rsync treats source directories differently based on the trailing slash:
With trailing slash (/)
rsync -a source_dir/ destination_dir/
Copies only the contents of source_dir.
Without trailing slash
rsync -a source_dir destination_dir/
Copies the entire directory into the destination.
This small detail can significantly affect your results.
Remote Synchronization with Rsync
Rsync must be installed on both local and remote machines. Modern versions use SSH by default.
Local to Remote Transfer
rsync -a /opt/media/ remote_user@remote_host:/opt/media/
If passwordless SSH is not configured, you’ll be prompted for the password.
Remote to Local Transfer
rsync -a remote_user@remote_host:/opt/media/ /opt/media/
Using a Custom SSH Port
If the remote server uses a non-default SSH port:
rsync -a -e "ssh -p 2322" /opt/media/ \ remote_user@remote_host:/opt/media/
Handling Large Transfers
For large or unstable transfers, use either screen/tmux or the -P option:
rsync -a -P remote_user@remote_host:/opt/media/ /opt/media/
This shows progress and allows resuming interrupted transfers.
Excluding Files and Directories
Exclude Using Directories
Use relative paths when excluding files.
rsync -a --exclude=node_modules --exclude=tmp \ /src_directory/ /dst_directory/
Exclude Using a File
rsync -a --exclude-from='/exclude-file.txt' \ /src_directory/ /dst_directory/
exclude-file.txt
node_modules tmp
Useful Rsync Tips
Dry Run (No Changes)
rsync -av --dry-run source/ destination/
Verbose Output
rsync -avv source/ destination/
Troubleshooting Common Rsync Issues
Permission Denied
- Check read/write permissions
- Verify SSH access
- Use -v for debugging
No Route to Host / Connection Refused
- Check network connectivity
- Verify firewall allows SSH
- Test with ssh user@host or ping
Protocol Version Mismatch
- Update Rsync on both machines using the package manager
Files Skipped or Not Transferred
- Check exclude rules
- Verify trailing slashes
- Use –dry-run to preview actions
Slow Transfers
- Avoid -z on fast networks
- Use -P for large files
Vanished Files Error
Occurs when files change during transfer.
- Re-run the command
- Use –ignore-existing if needed
For deeper debugging, run:
rsync -vv source/ destination/
Conclusion
Rsync is one of the most powerful and reliable tools for file synchronization and backups on Linux and Unix-based systems. Whether you’re copying files locally, syncing servers, or maintaining backups, Rsync offers unmatched efficiency and flexibility.
There’s much more to explore—check the official documentation using:
man rsync
If you have questions or need help with advanced Rsync use cases, feel free to leave a comment. Happy syncing!
