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.
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:
apt-get update
apt-get install -y rsyncOn Fedora:
dnf install -y rsyncOn Arch Linux:
pacman -Sy --noconfirm rsyncCopy a directory
Create sample data first:
mkdir -p /tmp/rs/src/docs
echo "hello" > /tmp/rs/src/readme.txt
echo "manual" > /tmp/rs/src/docs/manual.txtThe 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:
rsync -av /tmp/rs/src/ /tmp/rs/dest/
ls -R /tmp/rs/destThe 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.
rsync -a /tmp/rs/src /tmp/rs/nested/
ls /tmp/rs/nestedThe 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:
echo "new file" > /tmp/rs/src/new.txt
rsync -avni /tmp/rs/src/ /tmp/rs/dest/
ls /tmp/rs/destOnly 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:
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/destThe first command only reports stale.txt as deleted. The second one removes it.
Exclude files
--exclude=PATTERN skips matching files:
echo "log" > /tmp/rs/src/debug.log
rsync -av --exclude='*.log' /tmp/rs/src/ /tmp/rs/dest/
ls /tmp/rs/destSync 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
| Task | Command |
|---|---|
| Copy a directory’s contents | rsync -av SRC/ DEST/ |
| Preview changes | rsync -avni SRC/ DEST/ |
| Mirror, removing extras | rsync -av --delete SRC/ DEST/ |
| Skip files | rsync -av --exclude='*.log' SRC/ DEST/ |
| Push over SSH | rsync -av SRC/ user@host:DEST/ |
| Pull over SSH | rsync -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
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.