rsync
copy only the differencesrsync synchronises one directory with another. It compares the two sides and sends only the files — and the parts of files — that differ, which makes a repeat run nearly instant. It works locally and, over SSH, between machines.
cp copies everything every time; rsync copies only what changed.
At the terminal
Read the session first, then the notes: each numbered line in the output is answered by the note with the same number.
What the numbered lines are telling you
On a first run that is everything. The trailing slash on site/ means "the contents of site", not the directory itself.
Run the same command again and "sent" drops to a few hundred bytes: nothing has changed, so nothing is copied.
user@host:path sends over SSH. -n makes this a dry run: it reports what would happen and changes nothing.
old.html exists on the server but not locally, so a real run would delete it there. This is why the dry run comes first.
Switches
A switch changes how the command behaves. These are the ones used day to day; man rsync lists the rest. Single-letter switches can usually be joined, so -l -a may be written -la.
-a- Archive: recursive, and keep permissions, times, owners and links.
-v- Verbose: list each file transferred.
-h- Human-readable sizes.
-n- Dry run: show what would be done, change nothing.
-z- Compress data during transfer — helps on a slow link.
-P- Show progress and keep partial files, so a transfer can resume.
--delete- Remove files from the destination that are not in the source.
--exclude=PATTERN- Skip files matching the pattern.
Variations worth knowing
The same tool, pointed at a narrower question. These four cover most of what it is reached for day to day.
rsync -avh --progress big.iso user@192.0.2.10:Copies one large file to a remote home directory, showing progress.
rsync -avh --exclude ".git" project/ backup/Leaves out anything matching a pattern.
rsync -avhP user@192.0.2.10:/var/log/app/ logs/Pulls from a remote machine. -P keeps partial files so an interrupted copy can resume.
rsync -a --delete src/ dst/Makes dst an exact mirror of src, removing anything extra.
In cybersecurity
With --delete, rsync is a command that removes files on another machine.
"site/" copies the contents; "site" copies the directory. Combined with --delete, getting it wrong can empty the destination. Rehearse with -n.
The transfer is encrypted and authenticated by SSH. The old rsync:// daemon protocol is neither, and should not face the internet.
A mirror copies deletions and corruption faithfully. A backup needs older versions too — see --link-dest, or a real backup tool.