Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

rsync: synchronise and back up files

By updated

Improve this page

You want to copy a folder to a backup, or update it on a server, without sending everything again each time. Run without options, cp and scp copy everything; rsync compares the source and the destination file by file, only copies what changed, and deletes nothing until you ask. That is what makes it suited to backups and repeated deployments. Two traps call for method: one slash too many or too few moves the whole tree by one level, and --delete removes files without asking for confirmation.

This page teaches these steps on a practice folder, without touching your data. For a single copy to a server, scp is enough; to build an archive, see tar.

What you will learn

  • Choose between copying the content of a folder or the folder itself with the trailing slash
  • Simulate a command with -n and read the lines printed by -i
  • Know how rsync decides that a file changed, and force the comparison with -c
  • Delete the extra files with --delete, and limit the damage with --max-delete
  • Exclude files with --exclude and send to a server over SSH
  • Keep dated backups that share their unchanged files with --link-dest

Prerequisites

You know how to open a Linux terminal, create a folder and move into it with cd, list with ls, copy with cp and delete with rm. The exercises count lines with a pipe and grep -c or wc -l, and echo $? prints the last exit code. If rsync --version answers "command not found", install it: sudo apt install rsync (Debian, Ubuntu) or sudo dnf install rsync (AlmaLinux, Fedora). The SSH part assumes an account on a server: see the SSH client if you have never used one.

Create the practice folder

All the examples run in ~/rsync-lab. The block creates projet/ (the source) and copie/ (the destination) with four files: app.conf exists on both sides with different content, data.txt is identical, nouveau.txt only exists in projet/, vieux.log only exists in copie/. The touch -d commands set the dates so that rsync makes the same decisions on your machine as on the page. The names use French words, which the commands keep: projet is "project", copie "copy", nouveau "new", vieux "old", and later sauvegarde "backup", neuf "new", garde "keep", arbo "tree", vide "empty", essai "trial", lundi "Monday", mardi "Tuesday", restaure "restored", retour "return" and lire "read".

Terminal window
mkdir ~/rsync-lab && cd ~/rsync-lab && {
mkdir projet copie
printf 'port=8443\n' > projet/app.conf; printf 'port=80\n' > copie/app.conf
printf 'a\nb\n' | tee projet/data.txt > copie/data.txt
printf 'neuf\n' > projet/nouveau.txt; printf 'ancien\n' > copie/vieux.log
touch -d '2026-11-10 10:00' copie/*; touch -d '2026-11-20 10:00' projet/* copie/data.txt projet copie
}

The braces group the commands behind the &&: nothing runs if ~/rsync-lab already exists (see "Clean up the practice folder"). The outputs come from rsync 3.2.7 (Ubuntu 24.04); rsync --version | head -1 prints your version. Stay in ~/rsync-lab for the whole page.

The rsync command in detail

An rsync command is written in this order: the options, the source, then the destination. rsync only modifies the destination.

The essential options

Read the line from left to right: each number points to the explanation of that piece.

Anatomy of the command

Syntaxrsync [options] source/ destination/

  1. rsync the programIt makes the destination match the source.
  2. -a archive modeoptionalWalks the subdirectories and keeps dates, permissions and links.
  3. -i the detailsoptionalOne line per change. Without -i or -v, nothing is displayed.
  4. -n the simulationoptionalAnnounces without writing anything.
  5. --delete the deletionoptionalRemoves from the destination what is missing in the source.
  6. projet/ the sourceThe trailing slash means its content.
  7. copie/ the destinationThe only directory rsync changes.

Output1 deletion and 2 copies announced

Understand rsync in 2 minutes

rsync walks through the files of the source and of the destination, compares the size and the date of each one, then copies, skips or deletes. Across the network, it also splits the modified files into blocks, computes checksums and only sends the blocks that differ. Between two local folders, it copies the whole file again.

The map follows the practice folder: projet/ against copie/, with -i to see each decision. Before turning the options on, predict what rsync -ai projet/ copie/ does: which files are copied, which are skipped, what happens to vieux.log? Ask the same question with -n, then with --delete, then with both. Check on the map: look at the "Content of copie/" list and at the screen at the bottom.

Mental model

How rsync decides, file by file

look for the file on both sidescompare size and datecopy, skip or delete for each file in projet/ and copie/

$ rsync -ai projet/ copie/

  1. vieux.log exists in copie/ and not in projet/. Without --delete, rsync does not touch it: it stays.
  2. app.conf exists on both sides, but its size and date differ: rsync copies it. The old version in copie/ is replaced.
  3. data.txt has the same size and the same date on both sides: rsync skips it, without reading its content.
  4. nouveau.txt is missing in copie/: rsync creates it.
  5. Done: copie/ holds the files of projet/, plus vieux.log.

What the animation shows

  1. The source is on the left, the destination on the right. rsync writes only to the destination.
  2. Size and date are enough to consider a file identical: its content is not read.
  3. Without `--delete`, an extra file in the destination stays.
  4. With `-n`, the screen is the same, but nothing is written.

Golden rules

Simulate before --delete

Read the *deleting lines, then remove -n:

rsync -ai -n --delete projet/ copie/

Write the trailing slash of the source

With it, rsync copies the content of the directory, not the directory:

rsync -a projet/ copie/

Essential vocabulary

Choose · what gets copied

projet/
The content of the directoryrsync -a projet/ neuf/; ls neuf → 3 files in neuf/
projet
The directory itselfrsync -a projet neuf/; ls neuf → a projet directory in neuf/
--exclude
Leave out a patternrsync -ai --exclude='*.conf' projet/ copie/ → a single copy
-c
Compare contentsrsync -ai -c projet/ copie/ → app.conf and nouveau.txt

Control · what gets written or deleted

-n
Announce without writingrsync -ai -n projet/ copie/ → 2 copies announced
--delete
Delete extra filesrsync -ai --delete projet/ copie/ → 1 deletion, 2 copies
--max-delete
Limit deletionsrsync -a --delete --max-delete=0 projet/ copie/ > /dev/null 2>&1; echo $? → code 25
--exclude
An excluded file is not deletedrsync -ai --exclude='*.log' --delete projet/ copie/ → vieux.log stays

The trailing slash trap

A slash after the name of the source copies its content; without a slash, rsync copies the folder itself. The command succeeds in both cases and says nothing: it is the output of ls that shows the one-level shift. The slash on the destination has no effect.

Terminal window
rsync -a projet/ neuf/; ls neuf
rm -r neuf; rsync -a projet neuf/; ls neuf
Output
app.conf
data.txt
nouveau.txt
projet

With projet/, the three files arrive directly in neuf/. With projet, neuf/ only holds a projet folder, which holds the three files: the next copy would look for the files at the wrong level.

Two runs, two layouts Exercise 1 of 2 · ~8 min · terminal

A colleague runs rsync -a projet sauvegarde/ on Monday, then rsync -a projet/ sauvegarde/ on Tuesday, thinking they run the same thing twice. Replay their two commands in that order and look at what sauvegarde contains. Then redo two backups that update the same place, without an extra folder.

Replay the colleague's two commands

Run the two commands one after the other, then print the content of sauvegarde with ls -1 and count its files, subfolders included, with find sauvegarde -type f | wc -l.

Start over

Delete sauvegarde (this undoes the step: the folder was created by the exercise), then run the same command twice, the right one. Count again.

A hint 1 / 1

Each run put the files at a different place. Look at what is in sauvegarde next to the three files.

I have tried: show the solution
Terminal window
rsync -a projet sauvegarde/; rsync -a projet/ sauvegarde/
ls -1 sauvegarde; find sauvegarde -type f | wc -l
rm -r sauvegarde
rsync -a projet/ sauvegarde/; rsync -a projet/ sauvegarde/
ls -1 sauvegarde; find sauvegarde -type f | wc -l
Output of the colleague's command
app.conf
data.txt
nouveau.txt
projet
6
Output of the corrected command
app.conf
data.txt
nouveau.txt
3

On Monday, projet without a slash created sauvegarde/projet/ with three files; on Tuesday, projet/ dropped three more copies at the root of sauvegarde. sauvegarde therefore holds six files and one folder too many. With the slash on both runs, the second copies nothing more: the three files stay at the root.

Simulate with -n and read -i

-n (long form: --dry-run) does all the comparison work, prints what would be done and writes nothing. -i prints one line per change, with an eleven-character label in front of the name. Simulate the copy of projet/ to copie/:

Terminal window
rsync -ai -n projet/ copie/
Output
>f.st...... app.conf
>f+++++++++ nouveau.txt

Two files would be sent, data.txt is identical and does not appear. In >f.st......, > means the file arrives on your machine (< towards a server), f that it is a file, s that the size differs and t that the date differs. The + signs of >f+++++++++ mark a file that does not exist yet in the destination. A deletion is written *deleting.

With -v instead of -i, rsync only prints the names, preceded by deleting for a deletion, and the last line ends with (DRY RUN) when -n is active. -i keeps the reason for each line: prefer it for a simulation.

What rsync considers identical

rsync does not read the content of the files: it compares the size and the modification date. A file rewritten with the same number of bytes and the same date, to the second, is skipped silently. -c forces the comparison by checksum. To see it, prepare two copies of projet and change the content of data.txt in one of them, copying the date of the original with touch -r.

Terminal window
cp -a projet test-c1 && cp -a projet test-c2
printf 'x\ny\n' > test-c1/data.txt && touch -r projet/data.txt test-c1/data.txt
rsync -ai test-c1/ test-c2/
rsync -aic test-c1/ test-c2/
Output of the last command
>fc........ data.txt

The first command prints nothing: same size (4 bytes), same date, rsync does not look further. The second detects the difference in content, hence the letter c. -c rereads all the files on both sides: keep it for checking a suspect backup, not for a daily scheduled task.

Exact mirror with --delete

By default, a file present in the destination and missing from the source stays in place, like vieux.log in copie/. --delete removes it, without confirmation. Always simulate first and read the *deleting lines.

Terminal window
rsync -ai -n --delete projet/ copie/
Output
*deleting vieux.log
>f.st...... app.conf
>f+++++++++ nouveau.txt

One deletion and two copies are announced. Do not remove -n here: copie/ is still used further down, and the exercise will have you run the command on a copy.

The real safety net is --max-delete=N, which stops deletions beyond N files. Try it on a disposable copy with N at 0:

Terminal window
cp -a copie garde
rsync -ai --delete --max-delete=0 projet/ garde/; echo $?
Output
>f.st...... app.conf
>f+++++++++ nouveau.txt
Deletions stopped due to --max-delete limit (1 skipped)
rsync error: the --max-delete limit stopped deletions (code 25) at main.c(1356) [sender=3.2.7]
25

The copies took place, the deletion was refused, and rsync exits with code 25. In a script, this code lets you detect an abnormally empty source.

Swapping source and destination

rsync always copies what is on the left to what is on the right. Swapping the two overwrites your working files with the backup. The simulation shows it before the disaster:

Terminal window
rsync -ai -n --delete copie/ projet/
Output
*deleting nouveau.txt
>f.st...... app.conf
>f+++++++++ vieux.log

nouveau.txt would be deleted from projet/, and app.conf would take the value of the backup. The other classic mistake is an empty variable in a script: with SRC empty, "$SRC/" becomes /, the root of the disk.

Exclude files and directories

--exclude skips a pattern; rsync interprets it itself (shell wildcard syntax), so wrap it in quotes so that the shell does not expand it. An excluded file is neither copied nor deleted by --delete: with --exclude='*.log', vieux.log stays in copie/.

Terminal window
rsync -ai -n --exclude='*.log' --delete projet/ copie/
Output
>f.st...... app.conf
>f+++++++++ nouveau.txt

No *deleting line: vieux.log is protected by the exclusion. For long exclusion lists, or lists shared between scripts, --exclude-from=FILE reads one pattern per line (*.log, cache/, .git/).

Order of --include / --exclude

rsync applies the first rule that matches. Put --include before --exclude='*', and add --include='*/' so that it also enters the subfolders:

Terminal window
cp -a projet arbo && mkdir arbo/docs && printf 'x\n' > arbo/docs/lire.txt
rsync -ai -n --include='*.txt' --exclude='*' arbo/ txt/
rsync -ai -n --include='*/' --include='*.txt' --exclude='*' arbo/ txt/
Output of the first simulation
created directory txt
cd+++++++++ ./
>f+++++++++ data.txt
>f+++++++++ nouveau.txt
Output of the second simulation
created directory txt
cd+++++++++ ./
>f+++++++++ data.txt
>f+++++++++ nouveau.txt
cd+++++++++ docs/
>f+++++++++ docs/lire.txt

In the first one, --exclude='*' also skips the docs folder: lire.txt is missing, without a message. With --exclude='*' placed first, no file would be copied.

A mirror that keeps one file Exercise 2 of 2 · ~10 min · terminal

A backup script must make essai/ identical to projet/, except local.env, a settings file that only exists in the backup and must stay there. That day, the source disk is not mounted: the source folder is empty. Find out what the script would have erased, then write the right command.

Prepare essai and an empty folder:

Terminal window
mkdir vide && cp -a copie essai
touch essai/local.env essai/trace.log

Simulate the faulty command

Simulate a mirror from vide/ to essai/, and count the *deleting lines.

Fix and protect

Take projet/ as the source, protect local.env, and simulate until the list suits you.

Run and check

Run the command, list essai, then run the simulation again to check that nothing is left to do.

A hint 1 / 2

With an empty source, everything in the destination is "extra".

A hint 2 / 2

A file left out of the transfer is not deleted either.

I have tried: show the solution
Terminal window
rsync -ai -n --delete vide/ essai/ | grep -c deleting
rsync -ai -n --delete --exclude='local.env' projet/ essai/
rsync -ai --delete --exclude='local.env' projet/ essai/
ls -1 essai
rsync -ai -n --delete --exclude='local.env' projet/ essai/ | wc -l
Output of the second simulation
*deleting vieux.log
*deleting trace.log
.d..t...... ./
>f.st...... app.conf
>f+++++++++ nouveau.txt
Content of essai, then remaining lines
app.conf
data.txt
local.env
nouveau.txt
0

With the empty source, rsync would have erased the five files. With projet/ and the exclusion, it only deletes two. The .d..t...... ./ line flags the date of the folder itself. To start over: rm -r essai vide, then the preparation block.

Remote transfers over SSH

Add utilisateur@serveur: in front of the remote path (utilisateur is "user" and serveur "server" in French): rsync detects the colon and starts ssh, then starts a second rsync on the server. rsync must therefore be installed on both sides, and everything your SSH client can do (keys, ~/.ssh/config) applies with no option. The commands in this section need a server: replace admin@srv with your account. They were run from a Debian 13 (rsync 3.5.0) to an Ubuntu 26.04 (rsync 3.4.1). rsync is not installed by default on either of them, nor on AlmaLinux 10.

Terminal window
rsync -aiz projet/ admin@srv:projet/
rsync -aiz -n projet/ admin@srv:projet/
rsync -aiz admin@srv:projet/ retour/
Output of the first command
created directory projet
cd+++++++++ ./
<f+++++++++ app.conf
<f+++++++++ data.txt
<f+++++++++ nouveau.txt

The first command sends projet/ to the home folder of the remote account. The second checks: it prints nothing, so both sides are identical. The third brings the folder back. -z compresses during the transfer, useful on a slow link.

-e replaces the transport command, for another port or another key: -e "ssh -p 2222 -i ~/.ssh/sauvegarde_key" (sauvegarde is "backup"). The parent folder of the destination must exist on the server, otherwise mkdir ... failed: No such file or directory (2) and code 11; --mkpath creates it.

Only one end can be remote: a server-to-server transfer is refused with The source and destination cannot both be remote. and code 1. If a remote path contains a space, put the whole argument between quotes, without a backslash: rsync -av "user@srv:/srv/mes documents/" local/.

Resuming a transfer and limiting bandwidth

-P combines --partial and --progress: it keeps the partial file of a cut transfer, which serves as the basis for the next pass, and prints the progress. Tested on a 300 MB file cut at 20%: the rerun finished it, with the same digest on both sides. --bwlimit=1000 caps the rate at 1000 KiB/s (units of 1,024 bytes according to the manual). Do not use --append to resume a transfer: it adds to the end without checking the beginning, and can corrupt the file silently.

Each dated backup must look complete without taking the space of a full copy. With --link-dest=DIRECTORY, each file identical to the one in the reference folder becomes a hard link to it: both names point to the same data on the disk. A relative path is read from the destination.

Terminal window
mkdir sauvegardes
rsync -a projet/ sauvegardes/lundi/
printf 'neuf v2\n' > projet/nouveau.txt
rsync -a --link-dest=../lundi projet/ sauvegardes/mardi/
stat -c '%h %n' sauvegardes/*/*
Output of stat
2 sauvegardes/lundi/app.conf
2 sauvegardes/lundi/data.txt
1 sauvegardes/lundi/nouveau.txt
2 sauvegardes/mardi/app.conf
2 sauvegardes/mardi/data.txt
1 sauvegardes/mardi/nouveau.txt

The first column is the number of names that point to the same file. app.conf and data.txt show 2: Monday and Tuesday share the same data. nouveau.txt changed, Tuesday holds its own copy, and each version shows 1.

A hard link covers a whole file: a file changed by a single byte is copied in full. Never modify a file directly inside a backup: it would change in all the backups that share it. Deleting lundi does not break mardi, whose files stay in place.

To restore, copy the other way, first into an empty folder: rsync -ai sauvegardes/mardi/ restaure/. On the first run, the reference backup does not exist: rsync prints --link-dest arg does not exist, makes a full copy and exits with code 0.

Keeping rsync up to date: the 2026 security reflex

Several rsync flaws were fixed in 2026, mainly in versions 3.4.3 (20 May) and 3.5.0 (13 August); the project's NEWS file details them. Version 3.5.1 (21 September) fixes the regressions of 3.5.0. Update the package with sudo apt update && sudo apt install rsync or sudo dnf upgrade rsync.

The displayed number is not enough to judge a machine: Ubuntu 24.04 shows 3.2.7 and receives fixes without changing number. apt changelog rsync lists them. Go through SSH rather than the rsync network service (rsync://), which encrypts nothing.

Which command should you choose?

The choice mostly depends on one question: does the copy repeat?

NeedCommand
Repeated backup or deployment, resume after a cutrsync
One-off local copycp
Single copy to a serverscp
Browse a server, several files in one sessionsftp
A single compressed archive to sendtar then SSH
Two-way synchronisationunison: rsync is one-way

Troubleshooting

When a remote transfer fails, start by isolating the transport: ssh -v utilisateur@serveur "ls /chemin/" must succeed before any diagnosis on the rsync side. -vvv explains why a file is transferred again, --stats tells how much actually travelled, and echo $? gives the exit code: 23 for a partial transfer, 24 if source files vanished, 255 for an SSH failure.

Message or codeProbable causeSolution
mkstemp "..." failed: Permission denied (13), code 23No write rights on the destination, or SELinux/AppArmor blocksAim at an accessible folder, or --rsync-path="sudo rsync" if the server administrator has set it up
ssh: connect to host srv port 22: Connection refused, code 255sshd stopped, or wrong port (a firewall can give No route to host)Start sshd, open the firewall, or -e "ssh -p ..."
rsync: command not found, code 12rsync missing on the serverInstall it on the server as well
Host key verification failed, code 255Unknown server, or changed keyFirst connection: ssh user@server, compare the fingerprint, accept. Changed key: ssh-keygen -R server only if the reinstallation is confirmed
write failed on "...": No space left on device (28), code 11Disk full or quota reacheddf -h, free some space or raise the quota
rsync: [sender] change_dir "..." failed: No such file or directory (2), code 23Source missing or misspelledCheck the source path

Permission errors on the destination

rsync creates each file under a temporary name (.app.conf.XXXXXX) before renaming it. If the account cannot write in the destination, the terminal therefore prints a line rsync: [receiver] mkstemp "/opt/.app.conf.ms5nIm" failed: Permission denied (13) per file, then rsync exits with code 23. Do not confuse this message with the question of the owner: a non-root account cannot keep the owner, and -a then assigns the files to the current account without any error.

Clean up the practice folder

Delete the practice folder: all the copies of the page disappear with it.

Terminal window
cd ~
rm -r ~/rsync-lab

Key points

  • The source is on the left, the destination on the right, and rsync only writes in the destination. With a trailing slash, the content of the source is copied; without a slash, the folder itself is, and nothing flags the difference.
  • -n simulates and -i prints one line per change: >f+++++++++ for a new file, *deleting for a deletion.
  • rsync judges a file identical from its size and its date. A content change that alters neither is only detected with -c.
  • --delete removes what the source does not have: simulate first, read the *deleting lines, and limit the damage with --max-delete=N.
  • An excluded file is not deleted, and --include goes before --exclude '*'.
  • Over SSH, rsync must exist on both sides; -e sets the port and the key. With --link-dest, the unchanged files of a dated backup are hard links.

Knowledge check

Check that the essentials of this guide stuck. The questions cover only what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

6 questions
6 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer

FAQ: frequently asked questions