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: cp -c copies a big file instantly on a Mac terminal using APFS clones

Terminal
~/tips/tip-terminal-cp-c-clone

cp -c ~/VMs/ubuntu.img ~/VMs/ubuntu-before-upgrade.img

Makes a copy of a large file in a moment without using extra disk space.

Copying a 20 GB virtual machine before trying something risky can take minutes and doubles the space it uses. On APFS it doesn’t have to.

The -c flag of the macOS cp asks for a clone through clonefile. The copy shares its data blocks with the original until one of them changes:

cp -c ~/VMs/ubuntu.img ~/VMs/ubuntu-before-upgrade.img

A 200 MB test file cloned in a few milliseconds here, and the size of the file hardly matters. Free space barely changes. Only the blocks you later change in either copy take new space.

Finder does the same thing when you duplicate a file with Command-D, but in scripts I like being explicit. For whole folders, cp -c -R clones every file inside.

It only works within one APFS volume. Copy to another disk or a network share and cp quietly falls back to a normal copy, which is safe but slow. Also keep in mind what a clone is not: it is not a backup. If the disk dies, both copies are gone, because they live on the same blocks. And the space saving makes the Finder size misleading, since each clone shows the full size even though they share it.

Further reading: cp man page (macOS), About Apple File System (Apple Developer)

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