rsync

file transferLinux/Unix
The rsync command is one of the most frequently used commands in Linux/Unix-like operating systems. rsync The rsync command is a fast, versatile file copying and synchronization tool that efficiently transfers and synchronizes files between directories or across computers. It is widely used for backups, mirroring, and maintaining identical copies of data.

Quick Reference

Command Name:

rsync

Category:

file transfer

Platform:

Linux/Unix

Basic Usage:

rsync [options] [arguments]

Common Use Cases

  • 1

    File synchronization

    Keep directories in sync with minimal data transfer using delta algorithm

  • 2

    Remote backups

    Create and maintain backups on remote servers securely over SSH

  • 3

    Website mirroring

    Create exact copies of websites or directory structures

  • 4

    Large file transfers

    Transfer large files efficiently with the ability to resume interrupted transfers

Syntax

rsync [OPTION]... SRC [SRC]... DEST
rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC [DEST]

Options

Option Description
-v, --verbose Increase verbosity
-a, --archive Archive mode (equivalent to -rlptgoD)
-r, --recursive Recurse into directories
-b, --backup Make backups (see --suffix & --backup-dir)
--backup-dir=DIR Make backups into hierarchy based in DIR
-z, --compress Compress file data during the transfer
-h, --human-readable Output numbers in a human-readable format
--progress Show progress during transfer
--partial Keep partially transferred files
-n, --dry-run Perform a trial run with no changes made
-e, --rsh=COMMAND Specify the remote shell to use
--delete Delete extraneous files from destination directories
--exclude=PATTERN Exclude files matching PATTERN
--include=PATTERN Don't exclude files matching PATTERN
-P Same as --partial --progress
--ignore-existing Skip updating files that exist on receiver
--size-only Skip files that match in size
--update, -u Skip files that are newer on the receiver
--stats Give some file-transfer stats
--checksum, -c Skip based on checksum, not mod-time & size

Archive Mode (-a) Components:

Option Description
-r Recursive (copy directories)
-l Copy symlinks as symlinks
-p Preserve permissions
-t Preserve modification times
-g Preserve group
-o Preserve owner (super-user only)
-D Preserve device files and special files

Examples

How to Use These Examples

The examples below show common ways to use the rsync command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

rsync -a source/ destination/

Copy files from source to destination, preserving all attributes (-a = archive mode).

rsync -avz source/ destination/

Copy files in archive mode with verbose output and compression.

rsync -av --delete source/ destination/

Synchronize destination with source, deleting files in destination that don't exist in source.

Advanced Examples:

rsync -avz --progress source/ user@remote-host:/path/to/destination/

Copy files to a remote server with progress indication.

rsync -avz --exclude="*.tmp" source/ destination/

Copy files excluding those matching a pattern (*.tmp files).

rsync -avz --include="*.txt" --exclude="*" source/ destination/

Copy only specific files (only *.txt files in this example).

rsync -avz --dry-run source/ destination/

Perform a trial run with no changes made, showing what would happen.

rsync -avz -e "ssh -p 2222" source/ user@remote-host:/path/to/destination/

Use rsync with SSH on a non-standard port (2222).

rsync -avz --partial --progress large_file.iso user@remote-host:/path/to/destination/

Transfer a large file with the ability to resume if interrupted.

rsync -avz --backup --backup-dir=/path/to/backups source/ destination/

Make backups of files that would be overwritten or deleted.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Points:

  • The rsync command is a powerful file synchronization and transfer tool
  • It's known for its delta-transfer algorithm, which minimizes data transfer by sending only the differences between source and destination files
  • Archive mode (-a) is usually the best option for most backup and mirroring scenarios
  • Remote transfers are done over SSH by default in modern versions
  • The trailing slash in source paths is significant: with it, the contents of the directory are copied; without it, the directory itself is copied
  • The --delete option can be powerful but dangerous - use with caution and consider with --dry-run first
  • Rsync is available on most Unix-like systems and Windows (with Cygwin, WSL, or rsync for Windows)

Common Use Cases:

  • Creating and maintaining backups
  • Mirroring websites or directories
  • Transferring large files efficiently, especially over slow or unreliable connections
  • Deploying code to servers
  • Synchronizing data between computers
  • Implementing backup rotation systems
  • Performing incremental backups

Performance Optimization:

  • Use compression (-z) for transferring data over slow networks but avoid it on fast LANs where CPU might be the bottleneck
  • If transferring many small files, consider using --rsync-path="cd /target && rsync" to avoid stat calls
  • For large files that might be interrupted, use --partial to keep partially transferred files
  • Use --whole-file when the network is faster than disk I/O or when files are not likely to have common blocks
  • Consider using --bwlimit to limit bandwidth usage during transfers
  • For regular synchronization jobs, using --checksum can be more accurate but is CPU intensive

Safety Considerations:

  • Always use --dry-run (-n) first when using potentially destructive options like --delete
  • Consider using --backup with important data to create backups of overwritten files
  • Be careful with trailing slashes in source paths - they change the meaning of the command
  • When using --delete, consider combining with --exclude to protect important files
  • Add --inplace with caution as it updates files in-place instead of the safer method of writing to temporary files first
  • For critical data, consider using --checksum to ensure file integrity

Related Commands:

  • scp - Secure copy (remote file copy using SSH)
  • cp - Standard file copy command
  • mv - Move or rename files
  • tar - Create or extract archives, often used with rsync
  • ssh - Secure shell, the underlying transport for remote rsync operations
  • sftp - Secure file transfer protocol

Tips & Tricks

1

Use archive mode for most backups: rsync -a source/ destination/ preserves permissions, times, etc.

2

Add progress visibility: rsync -avP source/ destination/ shows detailed progress during transfer

3

Create exact mirrors with --delete: rsync -av --delete source/ destination/ removes files at destination that don't exist at source

4

Use --dry-run first to preview changes: rsync -av --dry-run --delete source/ destination/

5

Filter files with include/exclude: rsync -a --include="*.txt" --exclude="*" source/ destination/ to copy only text files

6

Limit bandwidth usage: rsync --bwlimit=1000 -a source/ destination/ limits to 1000 KB/s

7

Resume interrupted transfers: rsync -avP --partial large_file.iso user@server:/backup/

8

Create snapshot-style backups: rsync -a --link-dest=/path/to/previous/backup source/ /path/to/new/backup/

9

Efficient remote transfers: rsync -az -e "ssh -p 2222" source/ user@server:/destination/ uses compression and custom SSH port

Common Use Cases

File synchronization

Keep directories in sync with minimal data transfer using delta algorithm

Remote backups

Create and maintain backups on remote servers securely over SSH

Website mirroring

Create exact copies of websites or directory structures

Large file transfers

Transfer large files efficiently with the ability to resume interrupted transfers

Deployment automation

Deploy code and configurations to servers in automated workflows

Related Commands

scp

scp

View command

cp

cp

View command

mv

mv

View command

tar

tar

View command

ssh

ssh

View command

sftp

sftp

View command

dd

dd

View command

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the rsync command works in different scenarios.

$ rsync
View All Commands