29

How do i enable transparent filesystem compression on any APFS volume? Or check if it is already enabled?

I am expecting something similiar to NTFS compressed attribute. According to Wiki it should be supported.

Is it per file or volume ?

I am using Mojave 10.14.0, volumes without encryption.

2
  • 2
    Firstly, welcome to Ask Different! :) I hope you come to find this site has a lot to offer! In case you haven't already, it's worth taking the time to read the tour. All the best with your question.
    – Monomeeth
    CommentedMay 14, 2019 at 22:10
  • The answers here also have some relevant info: apple.stackexchange.com/questions/193893/…
    – andrew
    CommentedMay 5, 2020 at 22:06

2 Answers 2

32

The standard Mojave setup does have APFS compression implemented, but there's no user visible tools that allows you yourself to select files/folders for compression.

It seems that the "ditto" command supplied with macOS is supposed to be able to employ compression on APFS, but it only actually works with HFS+ file systems.

However, even though no user visible tools comes with Mojave - the developer level APIs are actually there. A third party utility exists that uses these APIs to provide a user tool for compressing files/folders:

https://github.com/RJVB/afsctool

You refer to the wikipedia page for your statement that APFS compression is supported. The wikipedia page actually refers to the above mentioned tool for that support.

You can install afsctool from Homebrew by this command:

brew install afsctool 

And you can install afsctool from MacPorts by this command:

sudo port install afscompress 

You can compress a file or folder like this:

afsctool -c filename 

where filename can be the name of a file or a folder.

You can check if a file is compressed, and how much, by this command:

afsctool -v filename 

The built-in compression feature of APFS is implemented in the same way as it was on HFS+. That support was introduced with OS X 10.6. Even though it has thus been a part of the macOS system for almost 10 years, it is not really widely used.

In my own experience it just works (HFS+ or APFS - doesn't matter). However, you might run into an edge case where some program reports the file size incorrectly or something like that. I haven't seen any such misbehavior yet. The whole idea with transparent compression is that user programs do not need to know that compression is used at all.

7
  • 3
    Ahm, "rarely implemented"? Run an afsctool -v /System/*/*/* (ie individual files, or wtf, the whole folder) and see that Apple uses this quite extensively, since inception. I think they also went from medium to max compression recently? [Then there are these strange & rare bugs: my grep binary fails after treatment, so do git repos after treating git, sometimes code-sigs fail.]CommentedMay 16, 2019 at 11:08
  • 4
    I didn't write that it was "rarely implemented". I write that it is not widely used - as in not widely used outside Apple. I.e. because there's no user visible tools supplied with the OS, very few people use it to compress their own stuff.CommentedMay 16, 2019 at 11:48
  • 3
    Note that the GitHub repo referenced in the answer does not install the same tool as the Homebrew recipe which is also referenced. Homebrew installs the original, apparently abandoned tool. The GitHub repo refers to a fork which is apparently still active. (I cannot personally vouch for either implementation.)
    – wjv
    CommentedJan 17, 2020 at 13:41
  • 4
    Your github-linked tool works with APFS, but the brew version does not. ditto works in any case.CommentedFeb 15, 2020 at 18:58
  • 2
    The command to install via MacPorts should be sudo port install afscompress, afsctool is the original tool by brkirch. See port info afscompress afsctool.CommentedAug 23, 2021 at 13:16
15

Q How do I enable transparent compression?

You don't. You can't. But keep reading.

Conceptual limitations

This is not "enabled" – for the whole filesystem – and then constantly applied, for example for newly created files. A user can only retroactively apply this filesystem compression by issuing certain direct commands, to individual files and folders. If those files get rewritten, recreated, the compression has to be re-applied 'manually'.

Using filesystem compression features

What you can do is compress transparently individual files and folders.
(One option for this would be using ditto or like in jksoegaard's answer, afsctool, alternative fork)

This distinction is important as it sets limits on what you can compress, effectively.

What to compress, and what not

You can compress your entire home-folder with this.
But that makes not much sense.

In part because many file types are already compressed. JPG, PNG, PDF, zip etc are all formats that usually do have some compression. Compressing them again doesn't save much space (if at all) but would add a redundant de-compression penalty on accessing them.

In part because how Apple implements its filesystems. Files do not get created and then added to if you amend the content. They get newly created and the old version destroyed. Newly created files don't get, and constantly changing files lose all benefit of transparent filesystem compression the next time they are written to/committed to the filesystem. This includes all temp-files, cache files, log-files, the document files you usually work on, many preferences etc.

Thus, you should only apply this to files that do not or rarely change.

Those that are read but not written. Application files are the prime candidate for this. And often a great candidate for space saving, as some developers have really atrocious packaging and deployment habits of waste and bloat included.

Tools to use

On older systems: Apart from afsctool and ditto, GUI tools to achieve this would be MoreSpace Folder Compression (seems to b ediscontinued) or Clusters (v1.7.2, 13$, last updated 2014, also with problems on newer systems).

That means only the commandline options are currently working:

The built-in ditto --hfsCompression will compress files, despite the option's name, on APFS.

afsctool will do that as well, but only an updated version/forkhttps://github.com/RJVB/afsctool does so without much problems. (As referenced in @jksoegaard's answer, only that the original works on 10.14, but too often not with files on APFS).

Note that the version homebrew offered was for a long time only the outdated version 1.6.4. That version has had problems with APFS and is not recommended. As of April 2022, homebrew also uses the forked version 1.7.2.

To get the newer/forked version that works well with APFS you need to install from source or resort to Macports for easier installation (and now homebrew as well).

On Macports however the binary installed for this fork is called afscompress!
(If you port install afsctool you'll get the outdated 1.6.4 version!)

That used to be the same with brew install afsctool. But since April 2022 the brewed afsctool works on APFS/Mojave+ — and with the name of the executable binary still as the regular afsctool !

In other words: Macports has a 'working fine on APFS' version called afscompress, and the outdated afsctool, homebrew has now also a good version with a name unchanged.

Precautions for unexpected consequences

Further, you have to be a bit careful in using this.

Unexpected results are very rare, but exist.

For example, a binary of grep ceases to function and compressing git results in your git-repos becoming unreadable unless you revert that compression (the repositories are still fine).

Some GUI applications do perform some 'homebrewed tamper detection' that might fail if they are compressed in this way. You might never run into one of those, but it's still best to have a backup ready and proceed in smaller steps, not by compressing all at once.


All subquestions answered:

Q How do I enable transparent filesystem compression on any APFS volume?

Tool of choice is now afscompress commandline tool.
To compress your Documents folder:

afscompress -c ~/Documents 

Q Or check if it is already enabled?

afscompress FILE would do this.

Output to expect:

 File is HFS+/APFS compressed. 

Q I am expecting something similar to NTFS compressed attribute. According to Wiki it should be supported.

It is, kind of, with some caveats.
Since OS 10.6.
But Apple has no GUI tools for a user to use it.
If a compressed file is re-saved again, exported etc, the new file will be uncompressed again.
So the compression used has to be re-applied.

Slightly outdated info here:
Unix commands for managing HFS+ compression in OS X

Q Is it per file or volume?

Per file.

Q I am using Mojave 10.14.0, volumes without encryption.

As long as you don't use the outdated afsctool, 10.14 is fine. APFS is fine. Encryption doesn't matter for this.

4
  • 2
    If the compression causes a binary to not work, or unreadable data, then it is not transparent. It is supposed to be transparent.CommentedNov 1, 2019 at 23:20
  • 1
    If a software causes errors, then it is not bug-free? Yet we expect it to be reasonably bug-free? — 'Transparent' means here just that user file operations are usually exactly the same for compressed and uncompressed files, that eg from Finder you cannot even see that a file is compressed in that way.CommentedFeb 18, 2020 at 11:26
  • 1
    TSBAA (this should be accepted answer), but people tend to choose simple "solutions".
    – poige
    CommentedFeb 2, 2021 at 11:13
  • 6
    @Sidias-Korrado The compression is not transparent on a file system level. APFS itself knows nothing about compression, neither does HFS+. It is transparent as long as you use standard file system APIs, though. Compressed files are just normal files with compressed content and a special extended file attribute set. This attribute is recognized by a file system extension in kernel that will then perform decompression during data reads. Yet there are a few APIs that bypass file system extensions and directly operate on the raw file data and those would see the compressed data.
    – Mecki
    CommentedJul 20, 2021 at 12:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.