Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Svelte Vue

Synchronizing Files with `rsync` over SSH

1 min read .
Synchronizing Files with `rsync` over SSH

rsync is a reliable tool for copying and synchronizing files locally or between machines. When the remote side is accessed through SSH, the transfer is encrypted and can use the same authentication model as normal SSH sessions.

Basic Remote Synchronization

A common command is:

rsync -avP /home/user/documents/ user@example.com:/home/user/backup/

The options are:

  • -a enables archive mode, preserving common metadata and recursively copying directories.
  • -v shows more detail while the transfer runs.
  • -P is shorthand for --partial --progress, preserving partially transferred files and displaying progress.

Modern rsync uses SSH by default for remote shell transfers on typical installations, so -e ssh is usually unnecessary unless you need custom SSH options.

The Trailing Slash Matters

These two source paths have different meanings:

rsync -avP /home/user/documents/ user@example.com:/home/user/backup/

This copies the contents of documents into backup.

rsync -avP /home/user/documents user@example.com:/home/user/backup/

This copies the documents directory itself under backup.

Using * as the source is generally less desirable because the shell expands it before rsync runs and hidden files are skipped.

Use a Custom SSH Port

If the SSH server listens on another port:

rsync -avP -e "ssh -p 2222" ./data/ user@example.com:/srv/data/

You can also configure the host, port, and identity file in ~/.ssh/config and keep the rsync command simpler.

Preview with Dry Run

Before a large or potentially destructive synchronization, use --dry-run:

rsync -avP --dry-run ./data/ user@example.com:/srv/data/

This reports what would be transferred without changing the destination.

Mirror Deletions Carefully

To make the destination remove files that no longer exist at the source, add --delete:

rsync -avP --delete ./data/ user@example.com:/srv/data/

--delete can remove remote files, so preview it with --dry-run first.

Compression

The -z option compresses file data during transfer:

rsync -avPz ./data/ user@example.com:/srv/data/

Compression can help over slow links when transferring compressible content, but it may waste CPU for already compressed formats such as ZIP archives, images, and videos.

Conclusion

rsync over SSH provides efficient, encrypted synchronization with resumable transfers and useful preview tools. Pay particular attention to source-path trailing slashes, prefer dry runs before destructive options, and only enable compression when it benefits the data and network involved.

chevron-up