Skip to main content
In this lesson, you’ll learn how to efficiently compress and decompress files on Linux using gzip, bzip2, xz, zip, and tar. Mastering these tools helps you save storage space and speed up file transfers across systems.

Table of Contents

  1. Compression Utilities Overview
  2. Basic Compression with gzip, bzip2, and xz
  3. Keeping Original Files
  4. Creating and Extracting zip Archives
  5. Combining tar with Compression
  6. Links and References

1. Compression Utilities Overview

Most Linux distributions include three primary compression tools by default: These utilities overwrite the original file with its compressed version unless you specify otherwise.

2. Basic Compression with gzip, bzip2, and xz

To compress a single file:
To decompress:
You can also use the long-form --decompress (or -d) flag:
By default, these commands remove the original file after compressing or decompressing.

3. Keeping Original Files

To retain the input files alongside the compressed versions, add the -k (or --keep) flag:
Check details of a compressed file using the --list (or -l) option:

4. Creating and Extracting zip Archives

The zip utility bundles and compresses files into a single archive. To compress individual files:
Recursively compress a directory (e.g., Pictures/):
Extract a .zip archive with unzip:
Unlike gzip, bzip2, and xz, the zip format natively supports multiple files and directories in one archive.

5. Combining tar with Compression

To package multiple files or directories and compress them in one step, use tar together with a compression option.

5.1 Creating a Tar Archive, Then Compressing

  1. Create a tarball:
  2. Compress it:

5.2 One-Step Creation and Compression

Use the appropriate flag to combine creation and compression:

5.3 Extracting Compressed Tarballs

tar auto-detects compression, so extracting is straightforward:
Overwriting existing files when extracting archives can lead to data loss. Always verify the contents before extraction or use --list (-t) to preview:

Watch Video