Fundamentals 6 min read

Master Linux Compression: Zip, Gzip, Bzip2, Tar.gz & Tar.bz2 Commands Explained

This guide walks through the most common Linux compression formats—.zip, .gz, .bz2, .tar.gz, and .tar.bz2—detailing their syntax, practical command examples, and how to compress, decompress, and inspect archives efficiently from the terminal.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux Compression: Zip, Gzip, Bzip2, Tar.gz & Tar.bz2 Commands Explained

Linux provides several compression formats, primarily .zip, .gz, .bz2, .tar.gz and .tar.bz2, each with its own command‑line utilities.

.zip, .gz, .bz2 formats

.zip syntax: zip archive_name source_file # compress a single file zip -r archive_name source_directory # compress a directory recursively unzip archive_name # extract a zip archive

Example: zip 11.zip 11.txt Compress an entire directory: zip -r coding.zip coding Compress only the directory entry without its contents: zip coding.zip coding.gz syntax (gzip): gzip source_file # creates source_file.gz and removes the original gzip -c source_file > target.gz # keeps the original file gzip -r directory # compresses all files in a directory (cannot compress the directory itself) gunzip archive_name.gz # decompress

.bz2 syntax (bzip2): bzip2 source_file # creates source_file.bz2 and removes the original bzip2 -k source_file # keeps the original file bzip2 -d archive_name.bz2 # decompress, keeping the compressed file bunzip2 archive_name.bz2 # alternative decompress command

.tar.gz and .tar.bz2 formats

Because .gz and .bz2 cannot directly compress directories, the tar utility is used to bundle files first, then compress the resulting .tar archive.

Tar packaging command: tar -cvf archive_name.tar source_file_or_dir Common options: -C (change directory), -V (verbose), -f (specify filename).

.tar.gz commands: tar -zcvf archive_name.tar.gz source_path # create a gzipped tarball (keeps source files) tar -zxvf archive_name.tar.gz # extract a gzipped tarball (keeps the archive) tar -ztvf archive_name.tar.gz # list contents without extracting

.tar.bz2 commands: tar -jcvf archive_name.tar.bz2 source_path # create a bzip2‑compressed tarball tar -jxvf archive_name.tar.bz2 # extract the bzip2 tarball tar -jtvf archive_name.tar.bz2 # list contents without extracting

These two formats ( .tar.gz and .tar.bz2) are the most widely used for compressing files and directories on Linux, and mastering their commands greatly simplifies packaging and distribution tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxGzipcompressionziptarbzip2
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.