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: ditto copies folders on a Mac and keeps their metadata

Terminal
~/tips/tip-macos-ditto-copy-with-metadata

ditto ~/Projects/site /Volumes/Backup/site

Copies files and folders on macOS and keeps resource forks, extended attributes and permissions.

cp -R copies the files, and on macOS that can mean losing some of the extra data attached to them. ditto is the Apple tool built for copying with that data intact.

ditto ~/Projects/site /Volumes/Backup/site

Unlike cp, ditto merges: if the destination folder already exists, the contents of the source are copied into it. That is closer to what people expect from a Finder copy than the trailing-slash rules of cp and rsync.

By default it preserves resource forks and HFS metadata. The man page says that since Mac OS X 10.4 this is the default behaviour. --norsrc turns it off, and that also disables extended attributes, ACLs and quarantine bits.

It can also make zip files the way the Finder does:

ditto -c -k --keepParent MyFolder MyFolder.zip

-c -k creates a PKZip archive, and --keepParent keeps the folder name at the top. Add --sequesterRsrc to put resource forks in a __MACOSX folder.

The caveat is that it is not rsync. Copying a large folder again copies everything again, and it does not offer the progress display or the include and exclude rules that rsync has. An Apple developer forum thread also reports that extended attribute handling is not always as expected on modern macOS, so check a sample file with xattr -l before relying on it for a backup.

Further reading: ditto(1) manual

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link