Ease sparse disk image compaction

Today's Best Tech Deals

Picked by Macworld's Editors

Top Deals On Great Products

Picked by Techconnect's Editors

One useful OS X feature is the ability to create disk images. A disk image is a single file that, when double-clicked, appears like a typical hard drive to the operating system. (Most software you download comes on a disk image.) Disk images a great way to store related files that you don’t need to use often, as they can be “out of sight” until you need them. They’re also useful if you want to store something securely, as you can create encrypted disk images that require a password to mount.

There are essentially two types of disk images—a normal disk image and a sparse disk image. With a normal disk image, the disk image will consume as much drive space as its size, even when empty—i.e. a 40MB normal disk image requires 40MB of drive space, even if you’ve never used it. Sparse disk images, on the other hand, are disk image files that require just as much space as their contents require. So a 40MB sparse disk image will only need 40MB of drive space if you’ve saved 40MB of data onto it. The disk image will grow automatically as space is required.

However, while sparse disk images can grow automatically as needed, they cannot shrink automatically. So if you add 40MB of data to the image, then delete that same data, the sparse disk image will still require 40MB of drive space. Using Terminal, you can shrink sparse disk images to free up this wasted space. Just cd to the directory containing the sparse image (make sure it’s not mounted), and then issue this command:

hdiutil compact "Name of file.sparseimage"

But using Terminal is a bit of a pain. A much nicer solution is to use Automator to build a Finder plug-in, so you can compact your sparse images via the Finder’s contextual menu. And it’s a very simple Automator workflow that needs to be built, consisting of only two steps.

Start by launching Automator, which you’ll find in /Applications -> Utilities. In the Library column, click on Finder, and then drag Get Selected Finder Items from the Action column into the blank work area on the right. For the second step, click on the Automator entry in Library, and then drag Run Shell Script into the work area, below the existing command. In the work area for the Run Shell Script action, set the Shell pop-up to /bin/bash (it should be that by default), and set Pass Input to “as arguments” (which is not the default). Where you see the existing script ( for f in…, etc. ), erase all of it, and replace it with this one line:

hdiutil compact "$@"

That’s it—that’s the entire workflow. Choose File -> Save as Plug-In, and in the new dialog that appears, name your workflow ( Compact sparse image , or whatever you want to see in the contextual menu), and make sure the Plug-in For pop-up is set to Finder (the default). Click Save and you’re done.

To use your workflow, select a sparseimage file in the Finder (make sure the disk image is not mounted), control-click on it, and then choose Automator -> Compact Sparse Image (or whatever you named your workflow) from the pop-up menu. The workflow will run (you’ll see its progress in the menu bar), and any free space on your sparse disk image will be released.

Note: When you purchase something after clicking links in our articles, we may earn a small commission. Read our affiliate link policy for more details.
Shop Tech Products at Amazon