Skip to main content
Managing file sizes is essential for saving disk space and speeding up transfers. In this guide, you’ll learn how to compress and decompress files and directories using the most common Linux utilities: gzip, bzip2, xz, zip, and tar.

Table of Contents


Compression Tools Overview

Linux distributions typically include these single-file compressors by default: Each tool replaces the original file with the compressed version unless instructed otherwise.

Compressing and Decompressing Single Files

Use the following commands to compress and decompress individual files:
Alternatively, use the long options for clarity:
By default, gzip, bzip2, and xz delete the original file after (de)compression. Use -k or --keep to preserve input files.

Preserving Original Files

To keep both the source and the compressed version, add the -k (keep) flag:
You can confirm the flag via:

Inspecting Compressed Archives

To view metadata (compressed size, uncompressed size, compression ratio), use the --list (-l) option:

Working with ZIP Archives

Unlike gzip/bzip2/xz, the zip utility can bundle multiple files or directories into a single archive:
To extract everything from a ZIP archive:

Combining tar with Compression

Since gzip, bzip2, and xz operate on single files, tar is used to first archive multiple files/directories, then compress the archive.

Two-Step Archiving

One-Step Archiving with Compression

Leverage tar’s built-in compression flags:
Or use auto-compression (-a) to match the extension:

Extracting Compressed Tarballs

tar will detect compression automatically:

References

Watch Video