rsync Cheatsheet
Basic Syntax
rsync [options] source destination
Common Options
-a
: Archive mode (recursive, preserve permissions, timestamps, symlinks, etc.)-v
: Verbose output-z
: Compress file data during the transfer-r
: Recursive mode (copy directories recursively)-u
: Skip files that are newer on the receiver-h
: Human-readable numbers--progress
: Show progress during transfer--delete
: Delete files in the destination that are not in the source-e
: Specify remote shell program (e.g.,-e ssh
)
Examples
Basic Local Copy
rsync -avh source/ destination/
- Copy all files and directories from
source
todestination
preserving permissions, timestamps, and symlinks.
Copy to Remote Server
rsync -avzhe ssh source/ user@remote_host:/path/to/destination/
- Copy files to a remote server using SSH.
Copy from Remote Server
rsync -avzhe ssh user@remote_host:/path/to/source/ destination/
- Copy files from a remote server using SSH.
Show Progress During Transfer
rsync -avh --progress source/ destination/
- Show progress of each file transfer.
Sync Directories (Bidirectional)
rsync -avzhe ssh --delete source/ user@remote_host:/path/to/destination/
- Sync directories and delete files in the destination that are not in the source.
Exclude Files or Directories
rsync -avh --exclude='*.log' --exclude='temp/' source/ destination/
- Exclude files matching
*.log
and the directorytemp/
.
Dry Run (Preview Changes)
rsync -avh --dry-run source/ destination/
- Show what would have been transferred without actually doing it.
Limit Bandwidth Usage
rsync -avh --bwlimit=1000 source/ destination/
- Limit bandwidth usage to 1000 KB/s.
Using a Non-Standard SSH Port
rsync -avzhe 'ssh -p 2222' source/ user@remote_host:/path/to/destination/
- Use a non-standard SSH port (e.g., 2222).
Preserve Hard Links
rsync -avH source/ destination/
- Preserve hard links in the transfer.
Copy Only Modified Files
rsync -avh --ignore-existing source/ destination/
- Copy only files that don’t exist at the destination.
Syncing with Checksum
rsync -avc source/ destination/
- Use checksum to compare files instead of mod-time and size.
Useful Tips
Trailing Slash on Source: The presence of a trailing slash on the source directory (
source/
) indicates that the contents of the directory should be copied, but not the directory itself. Without the trailing slash (source
), the entire directory is copied.Using Cron for Scheduled Syncs:
crontab -e
Add a cron job:
0 2 * * * rsync -avzhe ssh /local/path/ user@remote_host:/remote/path/
- This will run
rsync
every day at 2 AM.
- This will run
Logging: To log the output of
rsync
, use:rsync -avh source/ destination/ >> /path/to/logfile.log 2>&1
Advanced Options
--backup
: Make backups of (replaced) destination files.--backup-dir
: Make backups into this directory.--suffix
: Define a backup suffix (default is~
).--exclude-from
: Read exclude patterns from a file.--include
: Include files matching a pattern.--include-from
: Read include patterns from a file.--chown
: Change the user and/or group of the files to the specifiedUSER:GROUP
.
Example with Advanced Options
rsync -avh --backup --backup-dir=/path/to/backup --suffix=_old --exclude-from='exclude.txt' source/ destination/
- Backup replaced files to
/path/to/backup
with a suffix_old
, and exclude patterns listed inexclude.txt
.
This cheatsheet should help you get started with rsync
and cover most common scenarios and options you might need. Adjust the commands according to your specific requirements.