rsync is one of the most useful tools in a sysadmin’s kit. It copies and synchronizes files and directories locally or over SSH, and only transfers the parts of files that have changed – making it far more efficient than a plain cp or scp for repeated transfers.

These are the commands you’ll reach for most often.

Common Flags

The -avP combination is a solid default for most operations:

  • -a (archive) – preserves permissions, ownership, symlinks, and timestamps
  • -v (verbose) – shows what’s being transferred in real time
  • -P (progress + partial) – displays a progress bar and resumes interrupted transfers

1. Copy a Single File

rsync -avP /path/to/local/file.zip user@remote_server:/path/to/remote/directory/

2. Copy a Directory

The trailing slash matters here:

Copy the contents of a directory (files land directly in the destination):

rsync -avP /path/to/local/directory/ user@remote_server:/path/to/remote/directory/

Copy the directory itself (creates directory/ inside the destination):

rsync -avP /path/to/local/directory user@remote_server:/path/to/remote/directory/
The trailing slash decides whether rsync puts files in dest/ or in dest/src/
With a trailing slash on src, rsync copies its contents; without it, the src folder itself

3. Synchronize a Directory (Mirror/Backup)

Makes the destination an exact replica of the source – files deleted from the source are also deleted from the destination.

rsync -avP --delete /path/to/local/directory/ user@remote_server:/path/to/remote/directory/

Caution: --delete removes files at the destination that don’t exist at the source. Always do a dry run first (see below).

Anatomy of rsync -a –delete /source/ /destination/: archive mode, delete extras, trailing slash on source, destination, preview with -n
Reading an rsync command

4. Exclude Files or Directories

rsync -avP --exclude 'logs/*' --exclude 'cache/' /path/to/local/directory/ user@remote_server:/path/to/remote/directory/

--exclude accepts glob patterns and can be specified multiple times.

5. Dry Run

Simulates the transfer and shows exactly what would happen without making any changes. Essential before running anything with --delete.

rsync -avP --dry-run /path/to/local/directory/ user@remote_server:/path/to/remote/directory/

--dry-run and -n are equivalent.

6. Pull Files from a Remote Server

Reverse the source and destination to pull files down to your local machine:

rsync -avP user@remote_server:/path/to/remote/directory/ /path/to/local/directory/