X

Unix commands for managing HFS+ compression in OS X

A couple of days ago we discussed some problems that could arise with sizing files with HFS compression. This problem basically arises from there being a lack of consistency in the various utilities that are available in OS X, where some show compression status and size, and others show the uncompressed size.

Topher Kessler MacFixIt Editor
Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.
Topher Kessler
3 min read

A couple of days ago we discussed some problems that could arise with sizing files with HFS compression. This problem basically arises from there being a lack of consistency in the various utilities that are available in OS X, where some show compression status and size, and others show the uncompressed size.

If you regularly do scripting or other work with files on disk that require you to know details about the compression, there are some utilities available that can help you get the status of file compression, and apply or remove compression from files on disk.

Hfsdebug

Hfsdebug is a third-party command-line utility that outputs information stored in the file system's catalog and attributes files. To install it, download the precompiled executable from the developer's Web site, and copy it to the /usr/bin/ directory in the Terminal (this will allow it to be included in the Terminal's paths).

When the file is copied, run "sudo hfsdebug FILENAME" on a file, or optionally just type "sudo hfsdebug" followed by a single space, and then drag a file to the Terminal window and press enter. The command must be run with root permissions, but does all its operations in read-only mode.

hfsdebug can be downloaded here.

Afsctool

This utility is more oriented toward HFS+ compression than hfsdebug, and allows you to view the compression status of files and folders (files contained within) in OS X. In addition, it allows for some manipulation of these files including decompressing and compressing, listing only the compressed files, and managing compressed archives.

To install this command, copy it to the /usr/bin/ folder just like installing "hfsdebug."

afsctool can be downloaded here.

stat

The "stat" command is built into OS X, and allows you to get information about files and folders on the drive. The default values given are similar to the Get Info window and include the name, location, creation and modification dates, owner, group, permissions, and file size. In addition, you can use the utility to check for the compression status of a file.

To do this, enter the following command in the Terminal:

stat -f %f FILENAME

This command will output the file flags, which will be the number "32" if the file is compressed, and "0" if the file is uncompressed.

afscexpand

Another built-in command for managing HFS compression is "afscexpand," which was introduced in OS 10.6 and is used for uncompressing files in-place. The only other option for uncompressing files is to copy them either using the Finder or another utility.

ditto

The "ditto" command is included with OS X and is a utility for copying full directories and hierarchies. It can be used to copy individual files as well, and has options for applying file system compression to files, or at least maintaining compression when copying a compressed file to another location that supports compression.

To copy compressed files with ditto, just use the basic syntax in the following command:

ditto SOURCE DESTINATION

You can optionally supply the "--preserveHFSCompression" option to the program, but this is enabled by default. To remove filesystem compression, you can use other commands like "cp" or "rsync", but can also supply "ditto" with the "--nopreserveHFSCompression" option as follows:

ditto --nopreserveHFSCompression SOURCE DESTINATION

Additionally, you can use "ditto" to enable compression on files you are copying, by supplying the "--hfsCompression" option to the program when copying.

NOTE: Since we're looking at file system compression which is a way to save space, ditto can also be used to save space by removing unused code from universal binary applications. To do this, run the following commands to leave only the code for the designated architecture:

ditto --arch ppc PROGRAM NEWPROGRAM (PowerPC)

ditto --arch i386 PROGRAM NEWPROGRAM (32-bit intel)

ditto --arch x86_64 PROGRAM NEWPROGRAM (64-bit intel)



Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.