Apple changed their default file system from their Hierarchical File System (HFS+) to Apple File System (APFS) arround 2016 with the release of MacOS High Sierra. APFS is a modern “copy on write” file system but if you are used to a file system like ZFS it was a bit of a disapointment. The biggest issue most people had was the lack of tools provided by the system for managing and using the features of APFS. With time the situation improved but it is still not quite there yet. The commands below are tested on MAcOS Monterey. Some may work on older versions.
Copying Files
“copy on write” file systems make it easy to create clones of files or snapshots of the file system without using additional disk space, until changes are made.
Apple’s Finder always used this feature of APFS by cloning files rather than simply making a copy when a user “copied’ a file. This can save a huge amount of disk space if you copy large files. Cloned files share the same data on disk until a change is made to one of the files, which causes the files to differ but only for the changed blocks.
Unfortunately the commandline copy command cp
does not clone files by default. An option to clone a file rather than copy it was added though. If you use the -c
option to cp
it will clone the file(s) you are copying. e.g. cp -c <somefile> <anotherfile>
.
man cp
and man clonefile
will tell you more about this.
Snapshots
A snapshot preserves all the files on a APFS volume, as they appeared when the snapshot was taken. When a snapshot is initially created, it takes up no additional space on the volume but as changes are made to the volume the size of the snapshot grows. Creating a snapshot is extremely fast because only the file system structure is copied.
Apple uses snapshots when upgrading the OS or when making time machine backups. Please note snapshots are not backups as they are on the same volume as the original. They are however used to create consistent backups.
Time Machine performs the following snapshot management functions:
- Creates a snapshot of the startup volume approximately every hour, and keeps it for twenty-four hours before it is automatically removed.
- Creates a snapshot of the last successful Time Machine backup, and keeps it unless space is needed.
- Creates a snapshot before installing any macOS update.
Unfortunately a user can only create snapshots on their own by using the tmutil
commandline tool to create additional Time Machine snapshots. A user created snapshot will however be treated in exactly the same way as any other time machine snapshot. Your snapshot may be deleted at any time by time machine’s routine cleanup. This is a big disadvantage if you want to make a snapshot before making big changes but cannot rely on the snapshot to be preserved indefinitely.
To create a snapshot use the tmutil
command:
tmutil localsnapshot
creates a snapshot.
tmutil listlocalsnapshots <mount_point>
lists all snapshots.
tmutil deletelocalsnapshots [<mount_point> | <snapshot_date>]
will delete localsnapshots.
tmutil thinlocalsnapshots <mount_point> [purgeamount] [urgency]
will reclaim space on the disk without deleting all snapshots.
See man tmutil
for more information.