Commands.page Logo

How to Compress a Directory Using Brotli on Ubuntu

This guide explains how to compress directories using the Brotli algorithm on Ubuntu. You will learn how to install the necessary tools, bundle the directory into a tarball, apply Brotli compression, and decompress the files when needed. Follow the steps below for an efficient compression workflow.

Install Brotli

Before compressing files, you must install the Brotli utility on your system. Open your terminal and run the following command to update your package list and install the tool:

sudo apt update
sudo apt install brotli

Compress a Directory

Brotli compresses individual files rather than directories directly. To compress a folder, you must first bundle it into a tar archive and then compress that archive. You can perform this in a single step using the tar command with the --use-compress-program option.

Run the following command, replacing my_folder with your directory name and archive.tar.br with your desired output filename:

tar --use-compress-program=brotli -cf archive.tar.br my_folder

This creates a file named archive.tar.br containing the compressed data from your directory.

Decompress a Directory

To extract the contents of a Brotli-compressed tar archive, use the tar command with the extract flag. Ensure you specify the same compression program used during creation.

Execute the following command in your terminal:

tar --use-compress-program=brotli -xf archive.tar.br

This will restore the original directory and files to their current location.

Adjust Compression Levels

You can adjust the compression level to balance between speed and file size. Brotli supports levels from 0 to 11, where 11 provides the maximum compression. To use a specific level, modify the command as follows:

tar --use-compress-program="brotli -q 11" -cf archive.tar.br my_folder

Using higher levels may take longer to process but will result in smaller file sizes.