Skip to content

Menu

rsync Cheat Sheet: Flags, Trailing Slash, Excludes, scp and sftp

Checked against rsync, scp, sftp rsync 3.5.2 man page (4 Nov 2026); OpenSSH scp(1) and sftp(1) from man.openbsd.org on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for anyone who copies directories to servers or backup disks and wants to stop guessing about slashes and --delete. It follows the rsync 3.5.2 man page and the OpenSSH scp and sftp pages. rsync could not be run in this environment, so every row is checked against the docs only and marked untested.

When to use what

Use rsync for anything you will copy more than once. It compares files and only sends what changed, so the second deploy or backup is fast, and -n lets you preview the whole thing first. Use scp for a single quick copy when you don't care about re-running it. Use sftp when you want to browse a remote directory interactively or script a short list of uploads with -b.

The flag set to remember is -a (archive: recursive, keep links, permissions, times, group, owner and devices), -v to see the file names, and -z when the link is slow. Add -n every time you add --delete. I keep -n in shell history and remove it only after reading the list.

For host:path targets rsync normally runs over ssh, so the keys you already use for logging in are the ones it uses. The SSH key generator explains key types if you are setting one up, and SSH tunnels and port forwarding covers the cases where the server sits behind a jump host.

-a is -rlptgoD. It does not include ACLs (-A), extended attributes (-X) or hard links (-H).

rsync -av src/ backup/

Copy a directory tree locally, keeping permissions and times, listing each file

From the docs, not run by us
rsync -avn --delete src/ backup/

Dry run: show what would be copied and deleted without changing anything

From the docs, not run by us
rsync -av --delete src/ backup/

Make backup an exact mirror, removing files that no longer exist in src

Deletes on the receiving side. Run with -n first.

From the docs, not run by us
rsync -avz src/ host:/srv/app/

Compress data in transit, useful on slow links and wasted on a LAN

From the docs, not run by us
rsync -ai src/ backup/

Print a change summary per file (-i) so you can see why each file was sent

From the docs, not run by us
rsync -a --info=progress2 src/ dest/

Show one overall progress line for the whole transfer instead of per file

From the docs, not run by us
rsync -avP big.iso host:/data/

Same as --partial --progress: keep half-sent files so a rerun can resume

From the docs, not run by us
rsync -a --mkpath src/ host:/new/deep/dir/

Create missing directories on the destination before copying

From the docs, not run by us

Only the source's slash matters. A trailing slash means "the contents of this directory"; no slash means "this directory by name".

rsync -av /src/foo /dest

Creates /dest/foo and copies into it

From the docs, not run by us
rsync -av /src/foo/ /dest/foo

Same result as the line above, written with a slash on the source

From the docs, not run by us
rsync -av /src/foo/ /dest

Copies foo's contents straight into /dest, with no foo directory

From the docs, not run by us
rsync -ai foo/ bar/

Copy a directory's contents under a different name

From the docs, not run by us

Exclude patterns are matched relative to the transfer root. Test glob patterns with the glob pattern tester before trusting them with --delete.

rsync -av --exclude node_modules --exclude '.git' ./ host:app/

Skip directories by name anywhere in the tree

Try it: glob-pattern-testerFrom the docs, not run by us
rsync -av --exclude-from=.rsyncignore ./ host:app/

Read exclude patterns from a file, one per line

From the docs, not run by us
rsync -av --delete --delete-excluded ./ host:app/

Also remove files on the destination that now match an exclude rule

From the docs, not run by us
rsync -av -e 'ssh -p 2234' ./ host:app/

Pass options to ssh, here a non-standard port

From the docs, not run by us
rsync -av --max-size=100M src/ dest/

Skip any file larger than 100 MB

From the docs, not run by us
rsync -av --backup --backup-dir=../old src/ dest/

Move replaced or deleted files into a backup directory instead of losing them

From the docs, not run by us

sftp commands are from https://man.openbsd.org/sftp.1. Note the capital -P for the port in both scp and sftp, unlike ssh -p.

scp -r ./dist host:/srv/www/

Copy a directory recursively; scp follows symlinks it meets

From the docs, not run by us
scp -P 2222 file.txt host:/tmp/

Copy to a server on a non-standard SSH port

From the docs, not run by us
scp -i ~/.ssh/deploy_ed25519 app.tar.gz host:

Use a specific private key for this copy

Try it: ssh-key-generatorFrom the docs, not run by us
scp -p notes.md host:

Keep modification time, access time and mode bits

From the docs, not run by us
scp -O file host:/tmp/

Force the legacy SCP protocol for an old server without SFTP

From the docs, not run by us
sftp -P 2222 user@host

Open an interactive session; then ls, cd, get, put, bye

From the docs, not run by us
get -R logs/ ./logs

Inside sftp: download a remote directory recursively

From the docs, not run by us
sftp -b upload.txt host

Run sftp commands from a batch file, for simple scripted uploads

From the docs, not run by us

Worked example: Deploy a static site safely with a dry run

You build into dist/ and want the server's /srv/www/site to match it exactly, including removing pages you deleted. Start with a dry run and an itemized list:

rsync -ain --delete dist/ deploy@web1:/srv/www/site/

Read the output before anything else. Per the man page, each line of -i output starts with a code: a leading < means a file is sent to the remote host, a leading c means a local change or creation such as a new directory, and *deleting marks a file --delete would remove. If the list contains *deleting lines for files you expected to keep, your source path is wrong; the usual cause is a missing trailing slash on dist/, which would make rsync create site/dist and treat everything else as extra.

When the list looks right, run the same command without n:

rsync -ai --delete dist/ deploy@web1:/srv/www/site/

The second run of the same command should print nothing, because nothing differs. That empty output is your check that the mirror is in sync. These commands were not run here; this example follows the rsync 3.5.2 man page.

Common mistakes

  • Writing rsync -av dist host:/srv/site/ and ending up with /srv/site/dist.

    Put a trailing slash on the source (dist/) when you mean the directory's contents.

  • Adding --delete and running it for real the first time against the wrong destination.

    Run with -n and -i first, read the *deleting lines, then drop -n.

  • Using scp -p 2222 to set a port and getting a confusing error.

    scp and sftp use capital -P for the port; lowercase -p preserves file times.

  • Expecting -a to copy ACLs, extended attributes or hard links on a backup disk.

    Add -A, -X and -H explicitly; the man page lists them as excluded from -a.

  • Running -z between two machines on the same fast network and wondering why it is slower.

    Leave compression off on fast links; it costs CPU and only helps when bandwidth is the limit.