Use rsync to copy local files to local directories
Dry run first to see what would be copied without making any changes:
1
| rsync -avn source/ destination/
|
Then run without -n to execute:
1
| rsync -av source/ destination/
|
-a = archive mode (recursive, preserves permissions and timestamps). -n = dry run.
Will rsync only copy the different files?
Yes. By default rsync compares modification time + file size. If both match, the file is skipped. If either differs, it gets copied.
For a more thorough comparison, use --checksum — compares MD5 checksums instead of metadata. Slower but more accurate:
1
| rsync -av --checksum source/ destination/
|
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!