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
rsync -avn --delete src/ backup/Dry run: show what would be copied and deleted without changing anything
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.
rsync -avz src/ host:/srv/app/Compress data in transit, useful on slow links and wasted on a LAN
rsync -ai src/ backup/Print a change summary per file (-i) so you can see why each file was sent
rsync -a --info=progress2 src/ dest/Show one overall progress line for the whole transfer instead of per file
rsync -avP big.iso host:/data/Same as --partial --progress: keep half-sent files so a rerun can resume
rsync -a --mkpath src/ host:/new/deep/dir/Create missing directories on the destination before copying
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 /destCreates /dest/foo and copies into it
rsync -av /src/foo/ /dest/fooSame result as the line above, written with a slash on the source
rsync -av /src/foo/ /destCopies foo's contents straight into /dest, with no foo directory
rsync -ai foo/ bar/Copy a directory's contents under a different name
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
rsync -av --exclude-from=.rsyncignore ./ host:app/Read exclude patterns from a file, one per line
rsync -av --delete --delete-excluded ./ host:app/Also remove files on the destination that now match an exclude rule
rsync -av -e 'ssh -p 2234' ./ host:app/Pass options to ssh, here a non-standard port
rsync -av --max-size=100M src/ dest/Skip any file larger than 100 MB
rsync -av --backup --backup-dir=../old src/ dest/Move replaced or deleted files into a backup directory instead of losing them
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
scp -P 2222 file.txt host:/tmp/Copy to a server on a non-standard SSH port
scp -i ~/.ssh/deploy_ed25519 app.tar.gz host:Use a specific private key for this copy
scp -p notes.md host:Keep modification time, access time and mode bits
scp -O file host:/tmp/Force the legacy SCP protocol for an old server without SFTP
sftp -P 2222 user@hostOpen an interactive session; then ls, cd, get, put, bye
get -R logs/ ./logsInside sftp: download a remote directory recursively
sftp -b upload.txt hostRun sftp commands from a batch file, for simple scripted uploads
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
--deleteand running it for real the first time against the wrong destination.Run with
-nand-ifirst, read the*deletinglines, then drop-n.Using
scp -p 2222to set a port and getting a confusing error.scp and sftp use capital
-Pfor the port; lowercase-ppreserves file times.Expecting
-ato copy ACLs, extended attributes or hard links on a backup disk.Add
-A,-Xand-Hexplicitly; the man page lists them as excluded from-a.Running
-zbetween 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.
Related cheat sheets
Sources
- https://download.samba.org/pub/rsync/rsync.1
- https://download.samba.org/pub/rsync/NEWS.html
- https://man.openbsd.org/scp.1
- https://man.openbsd.org/sftp.1
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.