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

patch Command in Linux: Apply Diff Files and Reverse Changes

The patch command in Linux applies a set of changes from a diff file to one or…

7 minutes ago

basename Command in Linux: Strip Directory Paths and Suffixes

The basename command in Linux extracts the last component of a file path, removing the leading directory…

12 minutes ago

timeout Command in Linux: Kill Long-Running Commands Safely

The timeout command in Linux runs a command with a time limit and terminates it when the limit is reached. It is part of GNU coreutils, available on virtually every Linux distribution. It is most useful for commands with no built-in timeout option, such as ping, curl, tcpdump, or a custom script that might hang indefinitely. How the timeout Command Works in Linux The syntax is: bashtimeout [OPTIONS] DURATION COMMAND [ARG]...…

17 minutes ago

rmmod Command in Linux: Remove Kernel Modules and Blacklisting

The rmmod command in Linux removes a loaded module from the running kernel. Because the kernel has…

1 day ago

free Command in Linux: Check Memory Usage and Interpret Output

The free command in Linux gives you a quick summary of RAM and swap usage. It reads…

1 day ago

diff Command in Linux: Compare Files and Create Patches

The diff command in Linux compares two text files line by line and shows the lines that…

1 day ago