I've finally found how to enable compression in btrfs, and would like to do so. However, as my main drive, I'm not really wanting to risk a change that could corrupt the hard drive.

Everything I've read indicates that these options are safe to change, as they only apply on new writes, but I'd like some more assurance...

Can I have a rough explanation on how the compression algorithm works? Is it per file (as I think it has to be?)

After this is on, what options/commands are there to recompress the hard drive?


I can only answer with the available information in the different related btrfs sites. As you say, using compression is relatively safe as compression is only applied to the new written content. As per the stability of the FS, it is still in testing stage, although it seems to be pretty stable right now. About recompressing the whole disk, I'm not sure you can do it (I haven't seen it). It shouldn't be difficult to write such tool. Right now the obvious way is to copy/move the data from the partition to other disk/partition, and then re-copying, but it implies having enough space in another disk or partition to do the copy (and a fair amount of time)...


You can initiate a compression of a btrfs file system that has files written when it was mounted without compression by triggering a defragment:

sudo btrfs fi defragment /path

Note that path can be a directory or file within a btrfs volume, you don't need to do the whole thing at once. So you could target folders that contain highly compressible files, such as documents, ahead of folders that contain files that do not compress well, such as images, audio, video, and compressed archives (zip etc).


In case anyone who reads this wants to activate compression on a live, mounted btrfs filesystem without unmounting it, I just did the following and it seems to have worked:

1) Edit /etc/fstab to include compress=zstd in the options section of the line(s) for the filesystem(s). (I am using zstd compression, you may prefer a different compression algorithm.)

2) Run mount -o remount /. (Replace / with the mount point of your filesystem. In my case, it was /.)

Note: I actually have two btrfs subvolumes (/ and /home) on the same device. Compression appears to have been activated on both of them, even though I only ran mount -o remount / once. I had added compress=zstd to both lines in /etc/fstab.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.