File sync and migrations rsync Cheat Sheet banner

rsync Cheat Sheet

Use rsync to copy files, sync directories, perform dry-runs, migrate websites and build safer backup workflows.

Start here

Quick sync commands

Copy directory contents

rsync -av source/ destination/

Copy directory itself

rsync -av source destination/

Show progress

rsync -av --progress source/ destination/

Human-readable output

rsync -avh source/ destination/
Trailing slashes matter. source/ means contents of source. source means the directory itself.

Remote sync over SSH

Local to remote

rsync -avz public_html/ user@server:/home/user/public_html/

Remote to local

rsync -avz user@server:/home/user/public_html/ ./public_html/

Custom SSH port

rsync -avz -e "ssh -p 2222" source/ user@server:/path/

Use SSH key

rsync -avz -e "ssh -i ~/.ssh/id_rsa" source/ user@server:/path/

Dry-run before copying

Preview sync

rsync -av --dry-run source/ destination/

Preview delete sync

rsync -av --delete --dry-run source/ destination/
Safety: use --dry-run before --delete.

Exclude files and folders

TaskCommand
Exclude cachersync -av --exclude cache/ source/ destination/
Exclude logsrsync -av --exclude "*.log" source/ destination/
Use exclude filersync -av --exclude-from excludes.txt source/ destination/
Include only PHP filesrsync -av --include "*/" --include "*.php" --exclude "*" source/ destination/

Mirror/delete sync

Mirror destination

rsync -av --delete source/ destination/

Delete after transfer

rsync -av --delete-after source/ destination/
--delete removes files from the destination that do not exist in the source. Test first.

Backup and migration recipes

Backup website

rsync -avh /home/user/public_html/ /backup/user/public_html/

Backup with timestamp

rsync -avh public_html/ backups/public_html-$(date +%F)/

Migrate with compression

rsync -avz --progress public_html/ user@newserver:/home/user/public_html/

Copy permissions and ownership

rsync -a source/ destination/

Troubleshooting rsync

ProblemCheck
Permission deniedCheck SSH user, path ownership and write permissions.
Unexpected nested folderCheck whether you used source or source/.
Huge transferAdd --dry-run --itemize-changes to inspect changes.
Slow transferUse -z for compression over network links, but not always for local copies.
Sync workflows

rsync workflows for backups and migrations

Dry run first

rsync -avhn source/ destination/

Sync over SSH

rsync -avz -e ssh public_html/ user@example.com:/home/user/public_html/

Exclude cache

rsync -av --exclude cache/ source/ destination/

Mirror with delete

rsync -av --delete source/ destination/
Use --delete only when the destination should exactly mirror the source. Dry run first.
FAQ

Frequently Asked Questions

What does rsync -a do?

-a enables archive mode, preserving useful file attributes such as permissions and timestamps.

Should I use rsync dry run?

Yes, use -n or --dry-run before important syncs.

What does --delete do?

--delete removes destination files that do not exist in the source.

How do I use rsync over SSH?

Use rsync -e ssh or the default remote syntax user@host:/path.