Operations 7 min read

Master Linux File Compression: tar, gzip, bzip2, and zip Commands Explained

This guide walks you through the most common Linux compression tools—tar, gzip, bzip2, and zip—showing how to create archives, compress files, and extract contents with clear command examples and practical usage tips.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux File Compression: tar, gzip, bzip2, and zip Commands Explained

Compressing and decompressing files is a routine task in Linux, useful for reducing transfer size and saving storage space. The following sections detail the syntax and options for the most widely used compression utilities.

1. tar command

The tar utility bundles multiple files or directories into a single archive, typically with a .tar extension.

1.1 Create an archive

tar -cvf archive.tar file1 file2 file3

This creates archive.tar containing file1, file2, and file3. The -c flag creates the archive, and -v shows progress.

1.2 Archive a directory

tar -cvf archive.tar directory/

All files and sub‑directories under directory are packed into archive.tar.

1.3 Extract an archive

tar -xvf archive.tar

The -x flag extracts files, with -v displaying progress.

1.4 Extract to a specific directory

tar -xvf archive.tar -C /path/to/directory

The -C option changes to the target directory before extraction.

1.5 Combine compression with tar

tar -czvf archive.tar.gz file1 file2 file3

The -z flag compresses the archive using gzip, producing archive.tar.gz. To extract such an archive:

tar -xzvf archive.tar.gz

2. gzip command

gzip

compresses a single file to the .gz format.

2.1 Compress a file

gzip file1

Produces file1.gz.

2.2 Decompress a file

gzip -d file1.gz

Restores the original file1.

3. bzip2 command

bzip2

creates .bz2 archives, offering higher compression ratios.

3.1 Compress a file

bzip2 file1

Generates file1.bz2.

3.2 Decompress a file

bzip2 -d file1.bz2

Restores the original file1.

4. zip command

zip

creates archives compatible with Windows and other platforms, using the .zip extension.

4.1 Create a zip archive

zip archive.zip file1 file2 file3

Compresses the listed files into archive.zip.

4.2 Zip an entire directory

zip -r archive.zip directory/

The -r flag recursively adds all files and sub‑directories under directory.

4.3 Extract a zip archive

unzip archive.zip

Extracts all contents to the current directory.

4.4 Extract to a specific directory

unzip archive.zip -d /path/to/directory

Places the extracted files into the given path.

5. Summary

While many compression tools exist, mastering tar and zip covers most Linux use‑cases. Use tar for bundling large collections, gzip for fast, moderate compression, bzip2 when space savings are critical, and zip for cross‑platform sharing. Choose the tool that best fits your storage and transfer requirements.

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.

LinuxGzipcompressionzipcommand-linetarbzip2
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.