brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: rsync --mkpath creates the missing folders at the destination

Backup
~/tips/tip-rsync-mkpath

rsync -a --mkpath photos/2026/ [email protected]:/mnt/user/backup/photos/2026/

Lets rsync create every missing folder of the destination path, like mkdir -p.

rsync is picky about the destination path. It creates the last folder if it’s missing, but if anything above that doesn’t exist yet, the copy stops with an error. Since rsync 3.2.3 there’s a flag that makes it behave like mkdir -p.

rsync -a --mkpath photos/2026/ [email protected]:/mnt/user/backup/photos/2026/

Without --mkpath this fails when /mnt/user/backup/photos isn’t there yet. With it, rsync creates every missing part of the path on the receiving side and then copies as usual. That’s handy in a backup script that writes into a new folder per year, month or machine, where you’d otherwise need an extra ssh host mkdir -p ... first.

The trailing slash on the destination is worth a look when you copy a single file. With a slash, rsync treats the whole path as folders and puts the file inside:

rsync -a --mkpath backup.tar.gz [email protected]:/mnt/user/backup/2026/11/

Without the slash, the last part is taken as the new file name, so the file would land as a file called 11 in the folder 2026.

Now the caveat, and I think it’s a real one: the old behaviour is there on purpose. The error catches a typo in the destination path. With --mkpath a typo like /mnt/user/bakcup quietly creates a whole new tree and your backup goes there, so I’d only use it in scripts where the path is built for you, not when typing by hand. It also needs rsync 3.2.3 (August 2020) or newer. The rsync that ships with macOS isn’t the samba.org 3.x release, so check rsync --version on both machines, and if the flag is rejected, install a current rsync with Homebrew.

Further reading: rsync(1) manual, rsync NEWS (3.2.3 release)

More Backup tips restic snapshots --group-by host,paths mysqldump --routines --events mydb > mydb.sql rsync -a --files-from=list.txt /home/me/ backup:/backup/ restic find "*.txt" restic stats All Backup tips (45) All topics

Link