File sync in Linux: the rsync command
Updated November 1, 2026
rsync copies and syncs directories, sending only what has changed.
Syntax
rsync [options] source destinationCommon cases
rsync -a src/ dst/ # sync locally
rsync -av src/ dst/ # -v show what is copied
rsync -a src/ alice@server:/backup/ # to a remote server over ssh
rsync -an --delete src/ dst/ # -n dry run: what would change
rsync -a --delete src/ dst/ # make dst an exact copy of srcSample output
$ rsync -avn --delete src/ dst/
sending incremental file list
deleting stray.txt
data.txt
sent 101 bytes received 39 bytes 280.00 bytes/sec
total size is 298 speedup is 2.13 (DRY RUN)
$ rsync -a --delete src/ dst/
$ ls dst
README.md
data.txtHow it works: only the difference, and the trailing slash
rsyncfirst compares the source and destination and sends only the changed parts of files - so syncing a large directory again is fast. The-a(archive) flag keeps permissions, times and structure. One important subtlety is the trailing slash on the source:src/copies the directory's contents, whilesrc(no slash) copies the directory itself into the destination.--deleteremoves things in the destination that are gone from the source - powerful and dangerous, so run it with-nfirst (a dry run that changes nothing).
Common mistakes
rsync -an --delete src/ dst/ prints nothing and exits with 0. That does not mean there is nothing to change: without -v a dry run is always silent. Use rsync -avn --delete, where you see the deleting stray.txt line, the list of files to send, and the (DRY RUN) marker at the end.
rsync catches a source that does not exist: rsync: [sender] change_dir "/tmp/nosuch" failed: No such file or directory (2), exit code 23. It does not catch a source that exists and is empty: rsync -av --delete empty/ dst/ prints deleting data.txt, deleting README.md, leaves dst empty and exits with 0. A disk that failed to mount looks exactly like that to rsync.
One unreadable file does not stop rsync. In a directory where secret.txt has mode 000 it copies good.txt, prints rsync: [sender] send_files failed to open "/tmp/s/secret.txt": Permission denied (13) and finishes with exit code 23, which means some files were not transferred. The message scrolls away above the file list, so check $? in a script instead of the last line of output.
In the book
This topic is covered in full in the chapter Archives and backups.