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
-nand 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
--excludeand 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".
mkdir ~/rsync-lab && cd ~/rsync-lab && {mkdir projet copieprintf 'port=8443\n' > projet/app.conf; printf 'port=80\n' > copie/app.confprintf 'a\nb\n' | tee projet/data.txt > copie/data.txtprintf 'neuf\n' > projet/nouveau.txt; printf 'ancien\n' > copie/vieux.logtouch -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/
rsync-a-i-n--deleteprojet/copie/ -
rsyncthe programIt makes the destination match the source. -
-aarchive modeoptionalWalks the subdirectories and keeps dates, permissions and links. -
-ithe detailsoptionalOne line per change. Without-ior-v, nothing is displayed. -
-nthe simulationoptionalAnnounces without writing anything. -
--deletethe deletionoptionalRemoves from the destination what is missing in the source. -
projet/the sourceThe trailing slash means its content. -
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/
Try Files · projet/ and copie/
Compare, then act
Content of copie/
$ rsync -ai projet/ copie/
- vieux.log exists in copie/ and not in projet/. Without --delete, rsync does not touch it: it stays.
- app.conf exists on both sides, but its size and date differ: rsync copies it. The old version in copie/ is replaced.
- data.txt has the same size and the same date on both sides: rsync skips it, without reading its content.
- nouveau.txt is missing in copie/: rsync creates it.
- Done: copie/ holds the files of projet/, plus vieux.log.
What the animation shows
- The source is on the left, the destination on the right. rsync writes only to the destination.
- Size and date are enough to consider a file identical: its content is not read.
- Without `--delete`, an extra file in the destination stays.
- 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 directory
rsync -a projet/ neuf/; ls neuf→ 3 files in neuf/ projet- The directory itself
rsync -a projet neuf/; ls neuf→ a projet directory in neuf/ --exclude- Leave out a pattern
rsync -ai --exclude='*.conf' projet/ copie/→ a single copy -c- Compare contents
rsync -ai -c projet/ copie/→ app.conf and nouveau.txt
Control · what gets written or deleted
-n- Announce without writing
rsync -ai -n projet/ copie/→ 2 copies announced --delete- Delete extra files
rsync -ai --delete projet/ copie/→ 1 deletion, 2 copies --max-delete- Limit deletions
rsync -a --delete --max-delete=0 projet/ copie/ > /dev/null 2>&1; echo $?→ code 25 --exclude- An excluded file is not deleted
rsync -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.
rsync -a projet/ neuf/; ls neufrm -r neuf; rsync -a projet neuf/; ls neufapp.confdata.txtnouveau.txtprojetWith 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
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.
- rsync -a
- find -type f
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
rsync -a projet sauvegarde/; rsync -a projet/ sauvegarde/ls -1 sauvegarde; find sauvegarde -type f | wc -lrm -r sauvegardersync -a projet/ sauvegarde/; rsync -a projet/ sauvegarde/ls -1 sauvegarde; find sauvegarde -type f | wc -lapp.confdata.txtnouveau.txtprojet6app.confdata.txtnouveau.txt3On 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/:
rsync -ai -n projet/ copie/>f.st...... app.conf>f+++++++++ nouveau.txtTwo 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.
cp -a projet test-c1 && cp -a projet test-c2printf 'x\ny\n' > test-c1/data.txt && touch -r projet/data.txt test-c1/data.txtrsync -ai test-c1/ test-c2/rsync -aic test-c1/ test-c2/>fc........ data.txtThe 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.
rsync -ai -n --delete projet/ copie/*deleting vieux.log>f.st...... app.conf>f+++++++++ nouveau.txtOne 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:
cp -a copie gardersync -ai --delete --max-delete=0 projet/ garde/; echo $?>f.st...... app.conf>f+++++++++ nouveau.txtDeletions 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]25The 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:
rsync -ai -n --delete copie/ projet/*deleting nouveau.txt>f.st...... app.conf>f+++++++++ vieux.lognouveau.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/.
rsync -ai -n --exclude='*.log' --delete projet/ copie/>f.st...... app.conf>f+++++++++ nouveau.txtNo *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:
cp -a projet arbo && mkdir arbo/docs && printf 'x\n' > arbo/docs/lire.txtrsync -ai -n --include='*.txt' --exclude='*' arbo/ txt/rsync -ai -n --include='*/' --include='*.txt' --exclude='*' arbo/ txt/created directory txtcd+++++++++ ./>f+++++++++ data.txt>f+++++++++ nouveau.txtcreated directory txtcd+++++++++ ./>f+++++++++ data.txt>f+++++++++ nouveau.txtcd+++++++++ docs/>f+++++++++ docs/lire.txtIn 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
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:
mkdir vide && cp -a copie essaitouch essai/local.env essai/trace.logSimulate 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.
- rsync -n --delete
- --exclude
- grep -c
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
rsync -ai -n --delete vide/ essai/ | grep -c deletingrsync -ai -n --delete --exclude='local.env' projet/ essai/rsync -ai --delete --exclude='local.env' projet/ essai/ls -1 essairsync -ai -n --delete --exclude='local.env' projet/ essai/ | wc -l*deleting vieux.log*deleting trace.log.d..t...... ./>f.st...... app.conf>f+++++++++ nouveau.txtapp.confdata.txtlocal.envnouveau.txt0With 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.
rsync -aiz projet/ admin@srv:projet/rsync -aiz -n projet/ admin@srv:projet/rsync -aiz admin@srv:projet/ retour/created directory projetcd+++++++++ ./<f+++++++++ app.conf<f+++++++++ data.txt<f+++++++++ nouveau.txtThe 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.
Incremental backups with --link-dest
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.
mkdir sauvegardesrsync -a projet/ sauvegardes/lundi/printf 'neuf v2\n' > projet/nouveau.txtrsync -a --link-dest=../lundi projet/ sauvegardes/mardi/stat -c '%h %n' sauvegardes/*/*2 sauvegardes/lundi/app.conf2 sauvegardes/lundi/data.txt1 sauvegardes/lundi/nouveau.txt2 sauvegardes/mardi/app.conf2 sauvegardes/mardi/data.txt1 sauvegardes/mardi/nouveau.txtThe 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?
| Need | Command |
|---|---|
| Repeated backup or deployment, resume after a cut | rsync |
| One-off local copy | cp |
| Single copy to a server | scp |
| Browse a server, several files in one session | sftp |
| A single compressed archive to send | tar then SSH |
| Two-way synchronisation | unison: 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 code | Probable cause | Solution |
|---|---|---|
mkstemp "..." failed: Permission denied (13), code 23 | No write rights on the destination, or SELinux/AppArmor blocks | Aim 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 255 | sshd 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 12 | rsync missing on the server | Install it on the server as well |
Host key verification failed, code 255 | Unknown server, or changed key | First 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 11 | Disk full or quota reached | df -h, free some space or raise the quota |
rsync: [sender] change_dir "..." failed: No such file or directory (2), code 23 | Source missing or misspelled | Check 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.
cd ~rm -r ~/rsync-labKey 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.
-nsimulates and-iprints one line per change:>f+++++++++for a new file,*deletingfor a deletion.- rsync judges a file identical from its size and its date. A content change that alters neither is only detected with
-c. --deleteremoves what the source does not have: simulate first, read the*deletinglines, and limit the damage with--max-delete=N.- An excluded file is not deleted, and
--includegoes before--exclude '*'. - Over SSH, rsync must exist on both sides;
-esets 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
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
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Chrony and NTP: synchronise a server's clock
A new full quiz with random questions
Work again only on the questions you missed
FAQ: frequently asked questions
unison.rsync -av ~/docs/ /backup/docs/ # local
rsync -avz ~/data/ admin@srv:/backup/data/ # to a server, over SSH
cp and scp copy everything at each run. rsync only sends what changed since the last pass, which makes all the difference for a daily backup.| Need | Tool |
|---|---|
| One-off local copy | cp |
| One-off remote transfer | scp |
| Recurring backup or synchronisation | rsync |
| Large transfer over an unstable link | rsync -P |
-P), filters (--exclude), an exact mirror (--delete) and simulation (-n). For a single file copied once, cp or scp remain simpler.rsync [OPTIONS] SOURCE DESTINATION
The source is always on the left, the destination on the right: swapping them overwrites your data with an old backup. A remote path is written user@machine:/path, and only one of the two ends can be remote.rsync -av source/ destination/
rsync -avz source/ admin@srv:/backup/
-a (archive) groups several options: recursive copy, and preservation of permissions, modification times, symbolic links, special files, owner and group.A measured nuance: run by a non-root account, rsync cannot keep the original owner. The files then belong to the current account, without any error message. ACLs (-A) and extended attributes (-X) are not part of -a.rsync -av --exclude '*.log' --exclude 'cache/' source/ dest/
rsync -av --exclude-from=.rsyncignore source/ dest/
Wrap the patterns in quotes: rsync interprets them, not the shell.Rules apply in order, and the first one that matches wins. To copy only .txt files, write --include='*/' --include='*.txt' --exclude='*': the first pattern makes rsync enter the subfolders. In the reverse order, --exclude '*' catches everything: rsync copies no file and reports nothing.| Command | Result |
|---|---|
rsync -av ~/project/ /backup/ |
The content of project lands in /backup/ |
rsync -av ~/project /backup/ |
The directory lands as /backup/project/ |
rsync -avz ~/data/ admin@srv:/backup/data/ # upload
rsync -avz admin@srv:/var/log/ ~/srv-logs/ # download
rsync -avz -e "ssh -p 2222 -i ~/.ssh/backup_key" ~/data/ admin@srv:/backup/
rsync must be installed on both sides. If SSH fails, rsync prints the SSH message then connection unexpectedly closed and exits with code 255.--delete makes the destination identical to the source by removing what no longer exists there. A path mistake then becomes an erasure, without confirmation.rsync -avn --delete ~/source/ /backup/mirror/ # 1. preview
rsync -av --delete --max-delete=50 ~/source/ /backup/mirror/ # 2. run with a ceiling
Beyond the ceiling, rsync prints Deletions stopped due to --max-delete limit and exits with code 25.rsync -ai -n --delete source/ dest/
-n (--dry-run) runs the whole comparison and shows what would be copied or deleted (*deleting lines), without touching a single file. -i adds the reason for each copy: size, date or new file.rsync -avz --bwlimit=1000 source/ admin@srv:/backup/
According to the manual, the --bwlimit value is expressed in units of 1,024 bytes per second when no suffix is given: 1000 therefore caps at about 1 MiB/s. A suffix is accepted, for example --bwlimit=1.5m. It is useful not to saturate a shared link during a daytime backup.