rsync Command: Copy and Sync Directories Locally and Over SSH

Learn how rsync copies and mirrors directories, how the trailing slash works, how to preview changes with a dry run, and how to sync to a remote host over SSH.

Tools Published 3 min read

Contents

rsync copies files and directories and, on repeat runs, transfers only what changed. This guide shows how to copy a directory, mirror one with deletions, preview everything with a dry run, and push or pull over SSH.

Install rsync

It is often preinstalled, but minimal systems may lack it. On Debian and Ubuntu:

✓ tested · debian,ubuntu
apt-get update
apt-get install -y rsync

On Fedora:

✓ tested · fedora
dnf install -y rsync

On Arch Linux:

✓ tested · arch
pacman -Sy --noconfirm rsync

Copy a directory

Create sample data first:

✓ tested · debian,ubuntu,fedora,arch
mkdir -p /tmp/rs/src/docs
echo "hello" > /tmp/rs/src/readme.txt
echo "manual" > /tmp/rs/src/docs/manual.txt

The usual option is -a (--archive). The manual says it is equivalent to -rlptgoD: it recurses and preserves symlinks, permissions, times, group, owner and device files. It does not include ACLs, extended attributes or hard links. Add -v to list the transferred files:

✓ tested · debian,ubuntu,fedora,arch
rsync -av /tmp/rs/src/ /tmp/rs/dest/
ls -R /tmp/rs/dest

The trailing slash matters

A trailing slash on the source decides whether the directory itself or only its contents are copied. Without the slash, rsync -av /src/foo /dest creates /dest/foo. With it, the contents of foo are copied into the destination you name.

✓ tested · debian,ubuntu,fedora,arch
rsync -a /tmp/rs/src /tmp/rs/nested/
ls /tmp/rs/nested

The listing shows a src directory inside nested. Check the slash before every run, especially with --delete.

Preview with a dry run

-n (--dry-run) performs a trial run with no changes made. Combine it with -i (--itemize-changes) to get a change summary for each file:

✓ tested · debian,ubuntu,fedora,arch
echo "new file" > /tmp/rs/src/new.txt
rsync -avni /tmp/rs/src/ /tmp/rs/dest/
ls /tmp/rs/dest

Only new.txt is reported, and it does not appear in dest. Run the same command without -n to apply it.

Mirror with –delete

--delete removes files from the destination that no longer exist in the source. Always preview it first:

✓ tested · debian,ubuntu,fedora,arch
echo "stale" > /tmp/rs/dest/stale.txt
rsync -avn --delete /tmp/rs/src/ /tmp/rs/dest/
rsync -av --delete /tmp/rs/src/ /tmp/rs/dest/
ls /tmp/rs/dest

The first command only reports stale.txt as deleted. The second one removes it.

Exclude files

--exclude=PATTERN skips matching files:

✓ tested · debian,ubuntu,fedora,arch
echo "log" > /tmp/rs/src/debug.log
rsync -av --exclude='*.log' /tmp/rs/src/ /tmp/rs/dest/
ls /tmp/rs/dest

Sync over SSH

For a remote host, rsync uses a remote shell, normally ssh. The manual gives the forms [USER@]HOST:SRC to pull and SRC [USER@]HOST:DEST to push. These examples need a reachable SSH server, so they are not run in the test containers. Replace the user and host with your own.

Push a local directory to a server:

rsync -av /tmp/rs/src/ user@example.com:/srv/backup/src/

Pull it back:

rsync -av user@example.com:/srv/backup/src/ /tmp/rs/restored/

Use -e to pick the remote shell command, for example a non-default port:

rsync -av -e "ssh -p 2222" /tmp/rs/src/ user@example.com:/srv/backup/src/

Add -z to compress file data during the transfer. Use -P for --partial --progress: it keeps partly transferred files and shows progress, which should make a later transfer of the rest of a file much faster.

rsync -avzP /tmp/rs/src/ user@example.com:/srv/backup/src/

Quick reference

TaskCommand
Copy a directory’s contentsrsync -av SRC/ DEST/
Preview changesrsync -avni SRC/ DEST/
Mirror, removing extrasrsync -av --delete SRC/ DEST/
Skip filesrsync -av --exclude='*.log' SRC/ DEST/
Push over SSHrsync -av SRC/ user@host:DEST/
Pull over SSHrsync -av user@host:SRC/ DEST/

To see how much space a copy uses, read Check disk usage on Linux with df, du and ncdu. For a single compressed file instead of a synced tree, see tar Command: Create, List and Extract Archives on Linux.

Sources

  1. rsync project home page
  2. rsync(1) manual page

#Rsync #Ssh #Backup #Synchronization #Copy

This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.