How to Sync Files with rsync
The rsync command copies files while skipping anything that has not changed, which makes backups fast on repeat runs. The workhorse form is rsync -av source/ dest/. It can copy to another machine over SSH in the same command.
The basic rsync syntax
The common flags are -a (archive: copy recursively and preserve permissions, times, symlinks) and -v (verbose, so you see what is copied). The trailing slash on the source matters: source/ copies the folder's contents, while source copies the folder itself.
Mirror a folder with --delete
rsync by default only adds and updates; it never removes files that disappeared from the source. Add --delete to make the destination an exact mirror. This is powerful, so test with --dry-run first.
See what would change with --dry-run
Add -n (dry run) to preview the changes without touching anything. Combine with --delete to review a mirror before committing to it.
Sync over SSH
rsync uses SSH automatically when the destination has a host: user@host:/path. This deploys a folder to a server or pulls backups down.
The -z compresses during transfer, which speeds up remote copies. Set up SSH keys first so the sync runs without prompting.
Back up over time
Pair rsync with cron to back up on a schedule. A one-line nightly job with -a, --delete and an SSH destination gives you a rolling mirror with minimal bandwidth. See rsync for the full flag reference.
Keep learning
Related guides: How to Set Up SSH Keys for Passwordless Login · How to Use the ssh Command to Connect to a Remote Server · How to Compress and Extract Files with tar. Read all tutorials on the Learn Linux blog.