network

rsync

Efficient file synchronizer that copies only the differences between source and destination. Works locally or over SSH.

rsync [OPTIONS] SRC... DEST

Common flags

Flag Purpose
-a Archive: recurse and preserve permissions, times, symlinks, ownership
-v Verbose output
-z Compress data during transfer
-P Show progress and keep partially transferred files
--delete Delete files in DEST that no longer exist in SRC
--exclude Skip paths matching PATTERN
-n Dry run -- show what would change without doing it
-e Specify the remote shell (e.g. -e 'ssh -p 2222')

Examples

rsync -avP src/ backup/

Sync locally with progress; note the trailing slash on src/

rsync -avz --delete site/ user@host:/var/www/site/

Mirror a directory to a remote host and drop stale files

rsync -avn src/ backup/

Dry run to preview the sync

rsync -av --exclude='node_modules' proj/ user@host:/srv/proj/

Sync a project while skipping build junk

Gotcha

A trailing slash on the source (src/) copies its contents; without the slash (src) it copies the directory itself -- an easy way to nest an unintended level.

Related commands

← All Linux commands · Linux cheat sheet