Linux Comm Rsync
[ Linux Command Encyclopaedia](#)
* * *
## What is rsync Command
rsync (Remote Sync) is a powerful file synchronization tool in Linux systems that can efficiently synchronize files and directories between local or remote systems. rsync is known for its "incremental transfer" algorithm, which only transfers the differences between the source and destination files, greatly improving file transfer efficiency.
### Core Features of rsync
1. **Incremental Sync**: Only transfers changed parts of files, saving bandwidth and time
2. **Preserve Attributes**: Can maintain file permissions, timestamps and other metadata
3. **Compressed Transfer**: Supports data compression during transfer, reducing network load
4. **Flexible Exclusion**: Can exclude specific files or directories
5. **Remote Support**: Securely synchronize remote server files via SSH
* * *
## rsync Basic Syntax
rsync source destination
### Common Command Forms
**Local Sync**:
rsync -av /path/to/source/ /path/to/destination/
**Sync from Local to Remote**:
rsync -avz /local/path/ username@remote_host:/remote/path/
**Sync from Remote to Local**:
rsync -avz username@remote_host:/remote/path/ /local/path/
* * *
## Detailed Explanation of Common rsync Options
| Option | Full Form | Description |
| --- | --- | --- |
| -a | --archive | Archive mode, preserves all file attributes (equivalent to -rlptgoD) |
| -v | --verbose | Shows detailed transfer information |
| -z | --compress | Compresses data during transfer |
| -r | --recursive | Recursively copies directories |
| -l | --links | Preserves symbolic links |
| -p | --perms | Preserves file permissions |
| -t | --times | Preserves file modification times |
| -g | --group | Preserves file group |
| -o | --owner | Preserves file owner |
| -D | --devices | Preserves device files (superuser only) |
| -h | --human-readable | Outputs numbers in human-readable format |
| --progress | Shows transfer progress | |
| --delete | Deletes files in destination that don't exist in source | |
| --exclude=PATTERN | Excludes files matching PATTERN | |
| --include=PATTERN | Includes files matching PATTERN | |
* * *
## rsync Practical Application Examples
### Example 1: Local Directory Sync
## Example
# Sync /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 slash: Syncs directory contents
* Without slash: Syncs the directory itself
### Example 2: Remote Server Backup
## Example
# Sync local directory to remote server with compression
rsync -avz/data/backups/ user@example.com:/remote/backups/
### Example 3: Sync with Exclusion Options
## Example
# Sync but exclude .tmp files and log directory
rsync -av--exclude='*.tmp'--exclude='log/'/source//destination/
### Example 4: Keep Both Ends Fully Consistent (Delete extra files at destination)
## Example
rsync -av--delete/source//destination/
* * *
## Advanced rsync Usage
### 1. Using SSH with Custom 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 Frequently Asked Questions
### Q1: What is the difference between rsync and scp?
* **rsync**: Incremental sync, only transfers differences, suitable for regular backups
* **scp**: Full transfer each time, suitable for one-time file transfers
### Q2: How to resume an interrupted rsync transfer?
Use the `--partial` option to keep partially transferred files:
## Example
rsync -av--partial/source//destination/
### Q3: How to see what rsync will do without actually executing?
Use the `-n` (dry-run) option:
## Example
rsync -avn/source//destination/
### Q4: How to improve rsync transfer speed?
1. Use `-z` option to enable compression
2. Use `--compress-level=N` to adjust compression level (1-9)
3. Disable checksum calculation (only for trusted networks): `--no-checksum`
* * *
## Best Practice Recommendations
1. **Test First**: Use `-n` option to perform a simulation run first
2. **Logging**: Add `--log-file=rsync.log` to record operations
3. **Permission Management**: Consider using `--chmod` to unify file permissions
4. **Scheduled Tasks**: Combine with crontab to implement automatic backups
5. **Secure Transfer**: Always use SSH for remote transfers
* * *
## Summary
rsync is an essential tool for Linux system administrators and developers. Mastering it can help you:
* Efficiently complete 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 actual work and gradually explore more powerful features of rsync.
[ Linux Command Encyclopaedia](#)
YouTip