Wednesday, November 14, 2012

Natively Compressing Files on a Mac to Save Space

Since 10.6 (Snow Leopard) native file compression has been built into HFS+ (the MacOS Extended filesystem).  

The feature is hidden away in a command line utility called ditto (you can check the man pages for the command via "man ditto") which is a file/directory copying program.  

Apple hasn't recommended it for the following reasons but if you don't use computers prior to 10.6, you'll be fine.


     --hfsCompression
                   When copying files or extracting content from an archive,
                   if the destination is an HFS+ volume that supports compres-
                   sion, all the content will be compressed if appropriate.
                   This is only supported on Mac OS X 10.6 or later, and is
                   only intended to be used in installation and backup scenar-
                   ios that involve system files. Since files using HFS+ com-
                   pression are not readable on versions of Mac OS X earlier
                   than 10.6, this flag should not be used when dealing with
                   non-system files or other user-generated content that will
                   be used on a version of Mac OS X earlier than 10.6.



In it's simplist form, use it this way to compress a directory:

ditto  --hfsCompression sourceDirectory destinationDirectory

In my case I had a lot of applications that I didn't want to delete but that I might not ever use i.e.

sudo mv /Applications/Xbench.app /Applications/Xbench.app.old
sudo ditto  --hfsCompression /Applications/Xbench.app.old /Applications/Xbench.app
sudo rm -rf /Applications/Xbench.app.old

You can check the compression status with a utility like hfsdebug or fileXray or do a df -h before and after a compression to see that the disk storage used by the command is usually much less than without.