Commands.page Logo

How to Set Gzip Compression Level in Ubuntu Linux

This article details the process of configuring compression intensity when using the gzip utility on Ubuntu. It covers the numeric flags available for balancing speed against file size reduction and provides specific command examples for immediate use.

The gzip command allows you to specify a compression level using numeric flags ranging from 1 to 9. Level 1 provides the fastest compression with the least space savings, while level 9 offers the maximum compression ratio but takes the longest to process. If no level is specified, gzip defaults to level 6, which balances speed and size.

To set a specific compression level, add the desired number flag immediately after the gzip command. For example, to compress a file named data.txt using the fastest setting, run:

gzip -1 data.txt

To achieve the smallest possible file size regardless of processing time, use the maximum level:

gzip -9 data.txt

You can verify the compression ratio by checking the file size before and after execution using the ls -lh command. Note that compression levels only apply when creating archives; decompressing a file with gzip -d does not require a level flag as the data is simply restored to its original state.