# How do I use rsync over SSH, and see what it will do first?

Sync a folder to a server with rsync over SSH. Preview with --dry-run, get the trailing slash right, exclude files and use --delete with care. Real output.

Source: https://valakes.com/learn/rsync-over-ssh/

Last updated: 2026-11-25

Run rsync -ai --dry-run source/ user@host:/dest/ first. It lists what would change and changes nothing. Run the same command without --dry-run to do it. A slash after the source folder means its contents, and --delete removes files from the destination, so read the preview before you use it.

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), 25 November 2026.

Run `rsync -ai --dry-run site/ app:/var/www/site/` and read what it prints. The lines are the changes rsync would make, and nothing has changed yet. Run the same command without `--dry-run` to do it. The tool needs `rsync` on your computer and on the server, and it reaches the server with ssh, so your keys and your `~/.ssh/config` work as they do for `ssh` ([the config file article](https://valakes.com/learn/ssh-config-file/)).

## Preview with `--dry-run`

The flags are `-a` for archive mode, `-i` for a one-line summary of each change, and `-n`, the short form of `--dry-run`. Archive mode is the usual choice. It means `-rlptgoD`: recurse, keep links, permissions, times, group and owner (the owner only when you run as root), and copy device files. The local folder `site` has new files, and the server has an older `index.html` and two leftovers:

```console
$ rsync -ain site/ app:/var/www/site/
.d..t...... ./
<f+++++++++ about.html
<f+++++++++ debug.log
<f.st...... index.html
cd+++++++++ css/
<f+++++++++ css/app.css
cd+++++++++ node_modules/
cd+++++++++ node_modules/left-pad/
<f+++++++++ node_modules/left-pad/index.js
```

Read each line from the left. `<f` is a file that is sent to the server and `cd` is a folder that is created. Each `+` means the item is new. In `<f.st......` the `s` says the size differs and the `t` says the time differs. In `.d..t......`, a dot means the folder exists and only its time changes. The name follows.

## Do it, then do nothing

Take away `-n` to run it for real. The list is the same as the preview, line by line. Run it a second time and rsync has nothing to say:

```console
$ rsync -ai site/ app:/var/www/site/
.d..t...... ./
<f+++++++++ about.html
<f+++++++++ debug.log
<f.st...... index.html
cd+++++++++ css/
<f+++++++++ css/app.css
cd+++++++++ node_modules/
cd+++++++++ node_modules/left-pad/
<f+++++++++ node_modules/left-pad/index.js
$ rsync -ai site/ app:/var/www/site/
```

No output means no changes. rsync compares size and modification time first, so a repeat run is quick, and only the files that changed are sent.

## The trailing slash

A slash at the end of the source changes the result more than anything else in the command. Without it, rsync copies the folder by name. With it, rsync copies what is inside:

```console
$ rsync -ain site app:/tmp/copy/
created directory /tmp/copy
cd+++++++++ site/
<f+++++++++ site/about.html
<f+++++++++ site/debug.log
<f+++++++++ site/index.html
cd+++++++++ site/css/
<f+++++++++ site/css/app.css
cd+++++++++ site/node_modules/
cd+++++++++ site/node_modules/left-pad/
<f+++++++++ site/node_modules/left-pad/index.js
$ rsync -ain site/ app:/tmp/copy/
created directory /tmp/copy
cd+++++++++ ./
<f+++++++++ about.html
<f+++++++++ debug.log
<f+++++++++ index.html
cd+++++++++ css/
<f+++++++++ css/app.css
cd+++++++++ node_modules/
cd+++++++++ node_modules/left-pad/
<f+++++++++ node_modules/left-pad/index.js
```

The first command would make `/tmp/copy/site/`. The second would put the files straight into `/tmp/copy/`. Get this wrong with `--delete` and you can remove the wrong files, as the next sections show.

## Leave files out

`--exclude` takes a name or a pattern. It is matched against every name in the transfer. Leave out dependency folders and logs:

```console
$ rsync -ain --exclude=node_modules --exclude='*.log' site/ app:/tmp/copy2/
created directory /tmp/copy2
cd+++++++++ ./
<f+++++++++ about.html
<f+++++++++ index.html
cd+++++++++ css/
<f+++++++++ css/app.css
```

The `node_modules` folder and `debug.log` are gone from the list. Quote a pattern with `*` so that your shell leaves it for rsync.

## `--delete` makes the destination match the source

Without `--delete`, rsync adds and updates files and never removes anything. `--delete` also removes files from the destination that do not exist in the source. Preview it:

```console
$ rsync -ain --delete site/ app:/var/www/site/
*deleting   stale.txt
*deleting   old.html
```

Two files that exist only on the server would go. That is what you meant, if the server is meant to be a copy of `site`. Now the same command with a source that is empty, as happens with a mistyped path or a variable that holds nothing:

```console
$ mkdir empty
$ rsync -ain --delete empty/ app:/var/www/site/
*deleting   node_modules/left-pad/index.js
*deleting   node_modules/left-pad/
*deleting   node_modules/
*deleting   css/app.css
*deleting   css/
*deleting   stale.txt
*deleting   old.html
*deleting   index.html
*deleting   debug.log
*deleting   about.html
.d..t...... ./
```

Every file of the destination is on the list. With `-n` this was only a preview. Without it, the site would be gone. That is why you run `--delete` with `-n` first, and why you read the list when the number of lines is more than you expected. `--max-delete=NUM` adds a second net: rsync deletes at most that many files and then stops with an error.

Files that you exclude are not deleted on the destination either, unless you also pass `--delete-excluded`.

## Pull files from the server

Put the server first to copy the other way:

```console
$ rsync -ai app:/var/log/app/ logs/
.d..t...... ./
>f+++++++++ app.log
>f+++++++++ worker.log
```

The `>` at the start of the lines marks files received from the remote side.

## Another port, and a cut connection

The option `-e` tells rsync how to start its remote shell, and the value is a command with its options. For a server on port 2222:

```console
$ rsync -ai -e 'ssh -p 2222' notes.txt app:/tmp/
<f+++++++++ notes.txt
```

If the server always uses that port, write `Port 2222` in its block of `~/.ssh/config`, and rsync follows it without `-e`.

For a long transfer, `--partial` matters. By default, rsync deletes a file that was only partly transferred when the connection breaks. With `--partial`, it keeps the part, and the next run continues from it. `-P` means `--partial --progress`. A link that drops idle sessions may also need [keep-alive messages](https://valakes.com/learn/ssh-keep-alive/).

## Mistakes people make

You forget the trailing slash and get `site/site/`. Read the preview before the real run.

You run `--delete` without `-n`. Read the `*deleting` lines every time, especially after you change the source path.

You omit `-a`. Without `-r`, which `-a` includes, rsync skips folders.

You send files as one user and expect another owner. A normal user cannot give files away, so the owner on the server is the account you log in with. [The permissions article](https://valakes.com/learn/file-permissions/) covers `chown` and `chmod`.

You use rsync for a single small file. [scp](https://valakes.com/learn/copy-files-over-ssh/) is shorter for that.

## In Valakes

If you only need to move a few files without a terminal, the Files panel in Valakes does it over SFTP. It cannot resume a cut transfer, so keep rsync for the large ones. [Feature: Files](https://valakes.com/features/files/)

[Read the docs: Files](https://docs.valakes.com/files/)

## Questions

### Does rsync use SSH by default?

Yes. With a host name and a colon in the path, rsync starts ssh to reach the other machine. It needs rsync installed on both ends. To change the ssh options, such as the port, pass them with -e.

### How do I resume an interrupted rsync?

Run the same command again. rsync checks what is already there and sends the rest. Add --partial, or -P, so that a file that was cut in the middle is kept instead of deleted, and the next run continues from it.

### Is --delete safe?

Only after a dry run. It deletes everything in the destination that is not in the source, so a wrong or empty source empties the destination. Run it with -n first and read the lines that start with *deleting.

### Why does a slash change what rsync does?

A slash at the end of the source means the contents of the folder. Without it, rsync copies the folder itself, by name, into the destination. For a folder source, a slash on the destination changes nothing.

## References

- [rsync(1) on Ubuntu 24.04, options and the trailing slash](https://manpages.ubuntu.com/manpages/noble/man1/rsync.1.html)
- [rsync manual page at rsync.samba.org](https://rsync.samba.org/documentation.html)
- [ssh(1) on Ubuntu 24.04, the options rsync passes with -e](https://manpages.ubuntu.com/manpages/noble/man1/ssh.1.html)

## Related articles

- [How do I copy files to and from a server over SSH?](https://valakes.com/learn/copy-files-over-ssh/): Use scp notes.txt user@host:/tmp/ to upload and scp user@host:/var/log/app.log . to download. Add -r for a folder and -P for a port other than 22. For a transfer you may need to repeat, use rsync.
- [How do I change permissions recursively on Linux?](https://valakes.com/learn/file-permissions/): Do not use one number for the whole tree. Run find site -type d -exec chmod 755 {} + for folders and find site -type f -exec chmod 644 {} + for files, then chown -R to set the owner and group. chmod -R 777 lets every account on the machine change your files.
- [How do I stop SSH from dropping with "Broken pipe"?](https://valakes.com/learn/ssh-keep-alive/): Add ServerAliveInterval 30 to the Host * block of ~/.ssh/config. ssh then sends a small message every 30 seconds, so a router that forgets idle connections sees traffic, and ssh notices when the server stops answering.
