Compact sparse disk images via the command line

Jun 28, '04 10:20:00AM

Contributed by: SOX

As you may know Sparse Disk Images grow in size as needed automatically when you add files to the image. However they don't shrink in size when you remove files. This limits the utility of them for many purposes, such as maintaining an adaptable sized HFS partion on a USB thumb drive. Well it turns out shrinking those bloated sparse images is not a problem. Just unmount the image if it is mounted. Then pull up a Terminal window and type:

hdiutil compact /some_path/your.sparseimage
where /some_path/your.sparseimage is the path and file name of the sparse disk image. This will harmlessly reduce the sparse image down to the minimal size needed to contain the files within it. It does not erase the disk image and you won't lose any files doing this. As a side effect, it is likely the files in the image and the imagefile itself will be defragmented in the process. The process is very fast if the image is mostly empty.

Of course it is a swell idea to make a backup copy of the sparseimage before you compact it, just in case!

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040625012304236