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.
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 outputTrailing 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/
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 itVanished files — source files changed during the transfer; rerun the command or add --ignore-errors to continue past transient failuresSlow transfers — drop -z on fast networks; use -P for large files on slow linksProtocol version mismatch — update rsync on the older machine; both sides must run compatible versionsUse 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.
The patch command in Linux applies a set of changes from a diff file to one or…
The basename command in Linux extracts the last component of a file path, removing the leading directory…
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]...…
The rmmod command in Linux removes a loaded module from the running kernel. Because the kernel has…
The free command in Linux gives you a quick summary of RAM and swap usage. It reads…
The diff command in Linux compares two text files line by line and shows the lines that…