Operations 9 min read

Common Linux Compression and Archiving Tools: Commands and Usage

This article provides a comprehensive overview of the most frequently used Linux compression utilities (zip, gzip, bzip2, xz) and the tar archiving command, detailing their syntax, key options, how to combine them, and practical examples for compressing and extracting files and directories.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Common Linux Compression and Archiving Tools: Commands and Usage

This article summarizes the common compression tools available on Linux— zip , gzip , bzip2 , xz —and the tar archiving command, covering their basic commands, special parameters, and how to combine them for packaging and compression.

The primary compression commands and their associated file extensions are:

zip → .zip

gzip → .gz

bzip2 → .bz2

xz → .xz

Packaging commands include tar (producing .tar ) and its combinations with compression formats, such as .tar.gz / .tgz , .tar.bz2 , and .tar.xz . The compression ratio is calculated as (compressed size) / (original size).

Note that gzip , bzip2 , and xz can only compress single files; to compress directories you must use tar together with one of these tools or use zip , which can handle directories directly.

gzip

gzip compresses a file in place, creating <file_name>.gz . Use the -v flag to display the compression ratio.

gzip -v <file_name>

Decompress with:

gzip -d <file_name>.gz
gunzip <file_name>.gz

bzip2

bzip2 also compresses in place, producing <file_name>.bz2 . The -v flag shows the compression ratio.

bzip2 -v <file_name>

Decompress with:

bzip2 -d <file_name>.bz2
bunzip2 <file_name>.bz2

xz

xz compresses in place, creating <file_name>.xz . Use -v to see the ratio and -l to list compression details.

xz -v <file_name>
xz -l <file_name>.xz

Decompress with:

xz -d <file_name>.xz

zip

zip does not overwrite the original files. To compress a single file:

zip <compressed>.zip <file_name>

To compress multiple files:

zip <compressed>.zip a.txt b.txt c.txt

To compress a directory recursively:

zip -r dir.zip <dir_name>

Decompress with:

unzip <compressed>.zip

tar

tar is a packaging command that can also compress when combined with gzip , bzip2 , or xz . Core options include:

-c : create archive

-t : list archive contents

-x : extract archive

-z : filter through gzip

-j : filter through bzip2

-J : filter through xz

-f : specify archive file name

-v : verbose output

-C : change to directory before extracting

Examples:

tar -cvf target.tar a.txt b.txt c.txt
tar -cvf target.tar dir1 dir2
tar -cvf target.tar dir1 dir2 a.txt b.txt

Pack and compress with gzip :

tar -cvz -f target.tar.gz dir1 dir2 a.txt

Pack and compress with bzip2 :

tar -cvj -f target.tar.bz2 dir1 dir2 a.txt

Pack and compress with xz :

tar -cvJ -f target.tar.xz dir1 dir2 a.txt

List contents:

tar -tvz -f target.tar.gz
tar -tvJ -f target.tar.xz

Extract:

tar -xvz -f target.tar.gz

Extract to a specific directory using -C :

tar -xvz -f target.tar.gz -C ~/test

In summary, tar can bundle any files or directories into a single archive; by default extraction dumps the contents into the current directory, but the -C option allows extraction to a chosen path.

linuxcommand-linegzipcompressionXZtarBzip2
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login 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.