If you have an ssh access to a remote server. Deploying websites with rsync is a great option for simple sites and solo projects.
Here’s a concise rsync cheatsheet for quick reference:
Basic Syntax
1
| rsync [options] source destination
|
1
2
| - source: Path of the file or directory to copy from.
- destination: Path of the file or directory to copy to.
|
Common Options
Option | Description |
---|
-a | Archive mode; copies recursively and preserves permissions, times, symbolic links, etc. |
-v | Verbose; shows progress details. |
-z | Compresses files during transfer. |
-h | Outputs numbers in a human-readable format. |
–progress | Shows transfer progress of each file. |
–delete | Deletes files in the destination that aren’t in the source. |
-r | Recursive; used for directories. |
–exclude=’pattern’ | Excludes files that match the given pattern. |
-e | ssh Uses SSH for secure file transfer. |
Examples
- Basic Sync (Local)
1
| rsync -av source/ destination/
|
- Remote Sync (over SSH)
1
| rsync -avz -e ssh user@remote_host:/path/to/source/ /local/destination/
|
- Sync with Progress and Human-readable Size
1
2
|
rsync -avh --progress source/ destination/
|
- Delete Extraneous Files in Destination
1
2
3
|
rsync -av --delete source/ destination/
|
- Exclude Files
1
2
|
rsync -av --exclude='*.log' source/ destination/
|
- Dry Run (Shows what would happen without making changes)
1
2
|
rsync -av --dry-run source/ destination/
|
- Sync Only Files Newer in Source
1
| rsync -au source/ destination/
|
Advanced Usage
- Mirror Directories
1
2
|
rsync -avz --delete source/ destination/
|
- Copy Only File Structure (No File Content)
1
| rsync -av -f"+ */" -f"- *" source/ destination/
|
- Limit Bandwidth Usage
1
| rsync -av --bwlimit=1000 source/ destination/
|
Tips
- End with a Slash (/): Appending / to the source copies the contents of the directory, not the directory itself.
- Dry Run: Always use –dry-run to verify before executing destructive options (e.g., –delete).
- SSH Shortcut: Use -e ssh for encrypted transfers over SSH.
This cheatsheet covers the essentials to get you started with rsync for efficient file transfers and synchronization tasks!