Linux rsync Command
What is the rsync command
rsync (Remote Sync) is a powerful file synchronization tool in Linux systems. It can efficiently synchronize files and directories between local and remote systems. rsync is known for its "incremental transfer" algorithm, which transfers only the differences between source and target files, greatly improving file transfer efficiency.
Core features of rsync
- Incremental synchronization: Transfer only the changed parts of files, saving bandwidth and time
- Preserve attributes: Can preserve metadata such as file permissions and timestamps
- Compressed transfer: Supports compression during data transfer, reducing network load
- Flexible exclusion: Can exclude specific files or directories
- Remote support: Securely synchronize files on remote servers via SSH
Basic rsync syntax
rsync [选项] 源文件 目标文件
Common command forms
Local synchronization:
rsync -av /path/to/source/ /path/to/destination/
Synchronize from local to remote:
rsync -avz /local/path/ username@remote_host:/remote/path/
Synchronize from remote to local:
rsync -avz username@remote_host:/remote/path/ /local/path/
Detailed explanation of common rsync options
| Option | Full name | Description |
|---|---|---|
| -a | --archive | Archive mode; preserve all file attributes (equivalent to -rlptgoD) |
| -v | --verbose | Display detailed transfer information |
| -z | --compress | Compress data during transfer |
| -r | --recursive | Recursively copy directories |
| -l | --links | Preserve symbolic links |
| -p | --perms | Preserve file permissions |
| -t | --times | Preserve file modification times |
| -g | --group | Preserve file group ownership |
| -o | --owner | Preserve file owner |
| -D | --devices | Preserve device files (superuser only) |
| -h | --human-readable | Output numbers in a human-readable format |
| --progress | Show transfer progress | |
| --delete | Delete files in the destination that are not in the source | |
| --exclude=PATTERN | Exclude files matching PATTERN | |
| --include=PATTERN | Include files matching PATTERN |
Practical rsync examples
Example 1: Local directory synchronization
Example
Synchronize /home/user/docs to /backup/docs, preserving all attributes
rsync
-av
/
home
/
user
/
docs
/
/
backup
/
docs
/
Note: The trailing slash after the source directory / is important:
- With a slash: synchronize the contents of the directory
- Without a slash: synchronize the directory itself
Example 2: Remote server backup
Example
Synchronize a local directory to a remote server, using compression
rsync
-avz
/
data
/
backups
/
user
@
example.com:
/
remote
/
backups
/
Example 3: Synchronization with exclusion options
Example
Synchronize but exclude .tmp files and the log directory
rsync
-av
--exclude
=
'*.tmp'
--exclude
=
'log/'
/
source
/
/
destination
/
Example 4: Keep both sides exactly identical (delete extra files on the destination)
Example
rsync
-av
--delete
/
source
/
/
destination
/
Advanced rsync usage
1. Using a custom SSH port
Example
rsync
-avz
-e
'ssh -p 2222'
/
local
/
path
/
user
@
host:
/
remote
/
path
/
2. Bandwidth limit (limited to 500KB/s)
Example
rsync
-avz
--bwlimit
=
500
/
source
/
/
destination
/
3. Partial transfer (only transfer files larger than 100KB)
Example
rsync
-av
--min-size
=100K
/
source
/
/
destination
/
4. Scheduled backup script
Example
#!/bin/bash
rsync
-avz
--delete
/
important
/
data
/
backup
@
server:
/
backups
/
data
/
echo
"Backup completed at $(date)"
/
var
/
log
/
backup.log
rsync FAQ
Q1: What is the difference between rsync and scp?
- rsync: Incremental synchronization, transferring only the differences, suitable for regular backups
- scp: Full transfer every time, suitable for one-time file transfers
Q2: How to resume an interrupted rsync transfer?
Use --partial option to keep partially transferred files:
Example
rsync
-av
--partial
/
source
/
/
destination
/
Q3: How to see what rsync will do without actually executing it?
Use -n (dry-run) option:
Example
rsync
-avn
/
source
/
/
destination
/
Q4: How to improve rsync transfer speed?
- Use
-zoption to enable compression - Use
--compress-level=NAdjust the compression level (1-9) - Disable checksum calculation (trusted networks only):
--no-checksum
Best Practice Recommendations
- Test first: When using
-noption to perform a dry run first - Logging: Add the
--log-file=rsync.logLog operations - Permission management: Consider using
--chmodUnify file permissions - Scheduled Tasks: Combine with crontab for automated backups
- Secure transfer: Always transfer remotely over SSH
Summary
rsync is an essential tool for Linux system administrators and developers. Mastering it allows you to:
- Efficiently perform local and remote file synchronization
- Implement automated backup solutions
- Optimize network data transfer
- Maintain file system consistency
Through this article, you should have mastered the basic usage and advanced techniques of rsync. It is recommended to practice more in real work and gradually explore more of rsync's powerful features.
Linux Command Reference