Commands.page Logo

Compress Ubuntu Directory Highest Compression Level

This article provides a direct method for compressing directories on Ubuntu using the highest compression level available. It covers the necessary terminal commands using tar and xz to minimize archive size effectively. Readers will learn the exact syntax required to achieve maximum reduction in file size for backups or transfers.

To begin, open the terminal application on your Ubuntu system. Navigate to the parent directory containing the folder you wish to compress using the cd command. For example, if your directory is on the Desktop, type cd ~/Desktop.

The most effective tool for high compression pre-installed on Ubuntu is tar combined with xz. While tar usually defaults to a balanced compression level, you can force the maximum level by setting an environment variable. Run the following command:

XZ_OPT=-9 tar -cJf archive_name.tar.xz /path/to/directory

In this command, XZ_OPT=-9 sets the xz compressor to its highest setting. The -c flag creates a new archive, -J filters the archive through xz, and -f specifies the filename. Replace archive_name.tar.xz with your desired file name and /path/to/directory with the actual folder path.

If you require a faster compression process despite slightly larger file sizes, you can use gzip with the highest level. The command for this is:

tar -czf archive_name.tar.gz --gzip -9 /path/to/directory

To verify the compression was successful, list the files using ls -lh to see the reduced size. When you need to access the files again, extract the archive using the command tar -xJf archive_name.tar.xz for xz files or tar -xzf archive_name.tar.gz for gzip files.