How To

rsync Command in Linux: Sync Files, Mirror, and Transfer Remotely

The rsync command in Linux synchronizes files and directories between two locations, locally or over SSH. Unlike scp, which copies files unconditionally, rsync transfers only the differences between source and destination. This makes repeated syncs and large directory transfers far more efficient.

This guide covers how to use the rsync command in Linux with practical examples.

How the rsync Command Works in Linux

rsync is pre-installed on most Linux distributions and macOS. If missing:

bashsudo apt install rsync   # Ubuntu, Debiansudo dnf install rsync   # Fedora, RHEL

The general syntax is:

bashrsync [OPTIONS] SOURCE DESTINATION

Core options:

  • -a / --archive — archive mode. Equivalent to -rlptgoD: syncs recursively and preserves symbolic links, permissions, modification times, group, and ownership. Use this for almost every rsync job
  • -z / --compress — compresses during transfer. Only useful on slow connections. On fast networks, the CPU overhead outweighs the bandwidth savings
  • -P — combines --partial (keeps partially transferred files for resuming) and --progress (shows a per-file progress bar). Use this for large transfers on unreliable connections
  • --delete — deletes files at the destination that no longer exist at the source. Always run with --dry-run first
  • --dry-run — simulates without making any changes
  • -v — verbose output

Sync Directories, Mirror with –delete, and Remote Transfers

Trailing slash behavior is the most common source of mistakes with rsync:

bashrsync -a /src/ /dest/   # copies CONTENTS of /src into /dest/rsync -a /src /dest/    # copies the /src DIRECTORY itself into /dest/ → creates /dest/src/

Without the trailing slash on the source, rsync creates a subdirectory inside the destination. Always verify this before running a sync.

Mirror with –delete. Files at the destination not present at the source are permanently removed — there is no recycle bin. Always use --dry-run first:

bashrsync -a --dry-run --delete /var/www/html/ /var/www/html_backup/rsync -a --delete /var/www/html/ /var/www/html_backup/

Remote transfers. rsync uses SSH by default and must be installed on both machines:

bashrsync -a /opt/media/ user@host:/opt/media/          # local to remotersync -a user@host:/opt/media/ /opt/media/          # remote to local

For a non-standard SSH port:

bashrsync -a -e "ssh -p 2222" /opt/media/ user@host:/opt/media/

For large or interrupted transfers, -P keeps partial files and shows progress so you can resume where it stopped:

bashrsync -a -P user@host:/opt/media/ /opt/media/

Exclude Files and Directories

To exclude paths from the transfer, use --exclude with a path relative to the source directory, not the current working directory:

bashrsync -a --exclude=node_modules --exclude=tmp /src/ /dest/

For a long exclusion list, put the paths in a plain text file and use --exclude-from:

bashrsync -a --exclude-from='/exclude-file.txt' /src/ /dest/

Common troubleshooting:

  • Permission denied — check read on source and write on destination; add -v to see which file triggers it
  • Vanished files — source files changed during the transfer; rerun the command or add --ignore-errors to continue past transient failures
  • Slow transfers — drop -z on fast networks; use -P for large files on slow links
  • Protocol version mismatch — update rsync on the older machine; both sides must run compatible versions

Use rsync -a --dry-run before any destructive operation, especially before --delete. For remote backups, rsync -a -P is a reliable starting point. Leave a comment below if you run into any issues.

Cyber Defence

Recent Posts

How Swiss Privacy Rules Affect AI Companies Under GDPR

AI companies operating in or around Switzerland face a compliance challenge that most legal teams…

7 hours ago

Swiss FADP vs EU GDPR: Key Differences for AI and Data Privacy

Two major data privacy laws now govern how organizations handle personal data across Europe; the…

1 day ago

Kali Linux Commands Cheat Sheet: Complete Quick Reference

This cheat sheet covers the essential Kali Linux commands every pentester and ethical hacker uses…

1 month ago

What I Wish I Knew Before Learning Malware Analysis and Reverse Engineering

When I first started learning malware analysis and reverse engineering, I thought the hardest part…

1 month ago

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

1 month ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

1 month ago