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/

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).

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/