Rsync Command in Linux with Examples

By 

•

Updated on

•

10 min read

Using the rsync command to sync files and directories in Linux

Rsync is a fast, versatile command-line utility for synchronizing files and directories between two locations over a remote shell, or via a remote Rsync daemon. It provides fast incremental file transfer by transferring only the differences between the source and the destination.

You can use Rsync for mirroring data, incremental backups with --link-dest , copying files between systems, and as a superior alternative to scp , sftp , and cp commands.

This guide covers how to use Rsync through practical examples and detailed explanations of the most common Rsync options.

To assemble these options interactively, try our rsync command builder : check the flags you need and copy the finished command.

Installing Rsync

Rsync comes pre-installed on most Linux distributions and macOS. If missing, install it using your distribution’s package manager.

Install Rsync on Ubuntu, Debian, and Derivatives

Terminal
sudo apt install rsync

Install Rsync on Fedora, RHEL, and Derivatives

Terminal
sudo dnf install rsync

Rsync Command Syntax

Rsync commands follow this structure:

txt
# Local to Local:
rsync [OPTION]... [SRC]... DEST

# Local to Remote:
rsync [OPTION]... [SRC]... [USER@]HOST:DEST

# Remote to Local:
rsync [OPTION]... [USER@]HOST:SRC... [DEST]
  • OPTION: The Rsync options (detailed below).
  • SRC: Source path.
  • DEST: Destination path.
  • USER: Remote username.
  • HOST: Remote hostname or IP address.

Rsync Options

OptionDescription
-a, --archiveArchive mode; equivalent to -rlptgoD. Syncs directories recursively and preserves symbolic links, modification times, groups, ownership, and permissions. Ownership is preserved only when rsync runs as root on the receiving side.
-r, --recursiveCopies directories recursively. Already included in -a.
-p, --permsPreserves file permissions. Already included in -a.
-v, --verbosePrints detailed information about each transferred file.
-h, --human-readablePrints sizes in a human-readable format. When used alone, -h prints the help page.
-z, --compressCompresses data during transfer. Use only on slow connections; it adds unnecessary overhead on fast networks.
-PEquivalent to --partial --progress. Shows a progress bar and keeps partially transferred files. Useful for large files over slow or unstable connections.
--progressShows the transfer progress of each file.
-n, --dry-runSimulates the transfer without making any changes. Combine it with -v to see what rsync would do.
--deleteDeletes files from the destination that no longer exist at the source. Useful for mirroring.
-u, --updateSkips files that are newer on the destination.
-c, --checksumCompares files by checksum instead of size and modification time. Slower, but catches changes that keep the same size and timestamp.
-W, --whole-fileCopies whole files without the delta-transfer algorithm. This is the default for local transfers.
-x, --one-file-systemDoes not cross filesystem boundaries when recursing into directories.
-X, --xattrsPreserves extended attributes. Not included in -a.
-i, --itemize-changesPrints a short change summary for each file.
-q, --quietSuppresses non-error messages.
-eSpecifies the remote shell to use. Defaults to SSH.

Basic Usage

The most basic use case of Rsync is to copy a single file locally. Here is an example:

Terminal
rsync -a /opt/filename.zip /tmp/

The command prints nothing when the copy succeeds. The user running the command must have read permissions on the source location and write permissions on the destination.

Omitting the filename from the destination path copies the file with the current name. To save the file under a different name, specify the new name in the destination path:

Terminal
rsync -a /opt/filename.zip /tmp/newfilename.zip

The real power of Rsync comes when synchronizing directories. The example below shows how to create a local backup of website files:

Terminal
rsync -a /var/www/domain.com/public_html/ /var/www/domain.com/public_html_backup/

If the destination directory does not exist, Rsync will create it.

Rsync treats source paths with a trailing slash (/) differently:

  • With /: Rsync copies only the directory contents into the destination directory.
  • Without /: Rsync copies the source directory itself into the destination directory.

Mirror a Directory with –delete

Use the --delete option to make the destination an exact mirror of the source. Files at the destination that no longer exist at the source will be deleted:

Terminal
rsync -a --delete /var/www/domain.com/public_html/ /var/www/domain.com/public_html_backup/
Warning
--delete permanently removes files from the destination. Always run with --dry-run first to verify what will be deleted.

Common Option Combinations

Most rsync commands you will see in scripts and tutorials start with -a and add a few more options to it. Short options can be grouped together, so -avz is the same as -a -v -z.

The most common combination is -av. It runs rsync in archive mode and prints the name of each file it transfers. We will also add -h to show sizes in a human-readable format:

Terminal
rsync -avh src/ backup/

The output will look something like this:

output
sending incremental file list
created directory backup
./
about.html
index.html
css/
css/style.css
images/
images/logo.png

sent 123.14K bytes  received 144 bytes  246.57K bytes/sec
total size is 122.71K  speedup is 1.00

Rsync lists every directory and file it sends. The last two lines show the amount of data sent and received, the transfer speed, and the total size of the source files. The speedup is 1.00 here because the destination was empty and every file was copied in full. On the next run, only the changed files are transferred, and the speedup goes up.

When copying files to a remote host over a slow connection, add -z to compress the data in transit:

Terminal
rsync -avz src/ remote_user@remote_host_or_ip:/backup/

On a fast local network, compression usually slows the transfer down. In that case, use -av without -z.

For large files, use -avP. The -P option shows the progress of each file and keeps partially transferred files if the connection drops:

Terminal
rsync -avP /opt/filename.zip /tmp/
output
sending incremental file list
filename.zip
    524,288,000 100%    1.08GB/s    0:00:00 (xfr#1, to-chk=0/1)

sent 524,416,112 bytes  received 35 bytes  349,610,764.67 bytes/sec
total size is 524,288,000  speedup is 1.00

The progress line shows the number of bytes copied, the percentage, the current speed, and the time left. When the file is done, rsync replaces the time left with the elapsed time and adds xfr#1, the number of files transferred so far, and to-chk=0/1, the number of files left to check out of the total.

You may also see rsync -avp in older guides. The -p option preserves permissions, and it is already part of -a, so rsync -avp behaves the same as rsync -av.

Dry Run

Before running any rsync command that modifies data, especially one using --delete, use --dry-run (or -n) to simulate the transfer without making any changes. Always combine it with -v. Without it, rsync performs the dry run silently and prints nothing:

Terminal
rsync -avn --delete /src_directory/ /dst_directory/
output
sending incremental file list
deleting old.html
index.html
css/style.css

sent 224 bytes  received 43 bytes  534.00 bytes/sec
total size is 122,712  speedup is 459.60 (DRY RUN)

The deleting line shows a file that exists only on the destination and would be removed by --delete. The other two files would be copied. The (DRY RUN) marker at the end confirms that nothing on disk was changed. This is the safest way to verify the command before executing it for real.

Remote Synchronization

To transfer data remotely with Rsync, it must be installed on both the source and the destination machine. Rsync uses SSH as the default remote shell.

In the following example, we are transferring a directory from a local to a remote machine:

Terminal
rsync -a /opt/media/ remote_user@remote_host_or_ip:/opt/media/
Info
If you have not set up passwordless SSH login to the remote machine, you will be asked to enter the user password. For SSH connection flags and authentication options, see the ssh command guide .

To transfer data from a remote to a local machine, use the remote location as the source:

Terminal
rsync -a remote_user@remote_host_or_ip:/opt/media/ /opt/media/

If SSH on the remote host is listening on a port other than the default 22, specify the port using the -e option:

Terminal
rsync -a -e "ssh -p 2322" /opt/media/ remote_user@remote_host_or_ip:/opt/media/

For large transfers, it is recommended to run the rsync command inside a screen session or use the -P option to show progress and resume interrupted transfers:

Terminal
rsync -a -P remote_user@remote_host_or_ip:/opt/media/ /opt/media/

Excluding Files and Directories

There are two ways to exclude files and directories from an rsync transfer. The first is to use the --exclude option and specify paths on the command line.

When excluding files or directories , use their relative paths to the source location.

The following example excludes the node_modules and tmp directories:

Terminal
rsync -a --exclude=node_modules --exclude=tmp /src_directory/ /dst_directory/

The second option is to use --exclude-from and list the paths to exclude in a file:

Terminal
rsync -a --exclude-from='/exclude-file.txt' /src_directory/ /dst_directory/
/exclude-file.txttxt
node_modules
tmp

Quick Reference

For a printable quick reference, see the Rsync cheatsheet .

TaskCommand
Copy a file locallyrsync -a /src/file.zip /dest/
Sync a directory locallyrsync -a /src/ /dest/
Mirror (delete removed files)rsync -a --delete /src/ /dest/
Sync and list transferred filesrsync -av /src/ /dest/
Dry run (preview only)rsync -avn /src/ /dest/
Transfer to remotersync -a /src/ user@host:/dest/
Transfer from remotersync -a user@host:/src/ /dest/
Show progressrsync -a -P /src/ /dest/
Use custom SSH portrsync -a -e "ssh -p 2222" /src/ user@host:/dest/
Exclude a directoryrsync -a --exclude=dir /src/ /dest/
Exclude from filersync -a --exclude-from=list.txt /src/ /dest/
Compress during transferrsync -avz /src/ user@host:/dest/

Troubleshooting

Permission denied
Ensure read permissions are set on the source and write permissions on the destination. For remote transfers, verify that you have SSH access to the remote host. Add -v for verbose output to help diagnose the issue.

No route to host / Connection refused
Check your network connectivity, firewall rules (SSH port must be open), and confirm the correct hostname or IP address. Test connectivity with ping or by SSHing into the host directly with ssh user@host.

Protocol version mismatch
The full message is protocol version mismatch -- is your shell clean?. It is usually caused by a shell startup file on the remote host, such as ~/.bashrc, that prints text during login. Rsync reads that text as part of its own data stream. Run ssh user@host /bin/true > out.dat and check that out.dat is empty. If it is not, remove the echo or other command that prints the text.

Files skipped or not transferred
Add -v to see details. Check for active --exclude rules, trailing slash differences, or symlink issues. Use --dry-run to preview the transfer without making changes.

Slow transfers
Avoid -z on fast networks; compression adds overhead. For large files on slow or unreliable connections, use -P to show progress and resume interrupted transfers.

Vanished files error
This occurs when source files are deleted after rsync builds its file list but before they are copied. Rsync finishes the transfer and exits with code 24. The rest of the files were copied, so backup scripts can usually treat exit code 24 as a warning instead of a failure.

FAQ

What does the trailing slash on the source directory mean?
With a trailing slash (rsync -a /src/ /dest/), rsync copies the contents of src into dest. Without a trailing slash (rsync -a /src /dest/), rsync copies the src directory itself into dest, resulting in /dest/src/. This is one of the most common sources of confusion when using rsync.

How do I preview what rsync will do without transferring anything?
Add -n (--dry-run) together with -v to any rsync command, for example rsync -avn /src/ /dest/. Rsync prints the files it would transfer or delete without modifying anything on disk.

How do I keep the destination in sync and remove files deleted from the source?
Use --delete: rsync -a --delete /src/ /dest/. Always run with --dry-run first to confirm which files will be removed.

How do I resume an interrupted transfer?
Use the -P option (--partial --progress). It keeps partially transferred files and resumes them on the next run: rsync -a -P user@host:/src/ /dest/.

What is the difference between rsync and scp?
scp copies files unconditionally. rsync transfers only the differences between source and destination, making it far more efficient for repeated syncs or large directories. rsync also supports exclusions, dry runs, and mirroring, features scp does not have.

Conclusion

Rsync is the standard tool for efficient file synchronization in Linux, locally or over SSH. Use -a for archive mode, --dry-run to verify before applying, and --delete carefully when mirroring.

For more details, see the Rsync User’s Manual or our guide on excluding files and directories with rsync .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page