Fundamentals 6 min read

Master Linux Compression: gzip, bzip2, zip, xz & tar Commands

This guide provides a comprehensive overview of common Linux compression and decompression utilities—including gzip, bzip2, zip/unzip, xz, and tar—detailing their descriptions, key options, usage examples, and tips for handling files and directories efficiently.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux Compression: gzip, bzip2, zip, xz & tar Commands

Preface

Linux offers many compression and decompression tools; this article summarizes them for quick reference.

Compression and Decompression Commands Summary

1. gzip

Description: compression and decompression.
Usage: gzip [options]... [file]...
Options:
  -d  decompress
# gzip test.txt       # creates test.txt.gz
# gzip -d test.txt.gz # decompress gz file
Note: gzip can only compress files, not directories, and does not keep the original file.

2. bzip2

Description: compression and decompression.
Options:
  -d  decompress
# bzip2 test.txt       # creates test.txt.bz2
# bzip2 -d test.txt.bz2 # decompress bz2 file
Note: bzip2 can only compress files, not directories, and does not keep the original file.

3. zip & unzip

zip:

Description: compression.
Options:
  -r  recursive, include subdirectories
  -o  set archive timestamp to newest file
  -q  quiet mode
# zip -ro data.zip /opt   # package all contents of /opt into data.zip

unzip:

Description: decompression.
Options:
  -d<directory>  specify output directory
  -l  list archive contents
  -q  quiet mode
# unzip -d /root/te/ data.zip   # extract to /root/te
# unzip -l data.zip            # list files in archive

4. xz

Description: similar to bzip2 and gzip but higher compression ratio.
Options:
  -d  decompress
  -k  keep original file (default deletes)
  -f  force execution
# xz test.txt       # creates test.txt.xz
# xz -d test.txt.xz # decompress

5. tar

Description: archiving and extraction.
Options:
  -c  create archive
  -x  extract archive
  -t  list archive contents
  -r  append files to archive
  --delete  delete files from archive
  -u  update files in archive
The above commands are independent; only one can be used at a time, but they can be combined with other commands.
-z   use gzip for compression/decompression
-j   use bzip2 for compression/decompression
-v   verbose output
-O   write output to standard output
-C   specify extraction path
-f   specify archive name (must be last option)
The -f option must be the last parameter and is followed by the archive name.
# Compress:
 tar -cvf jpg.tar *.jpg
 tar -czvf jpg.tar.gz *.jpg
 tar -cjvf jpg.tar.bz2 *.jpg
 tar -tf jpg.tar.bz2
 tar -f te.tar -r te.txt
 tar --delete te.txt -f te.tar

# Decompress:
 tar -xvf file.tar
 tar -zxvf file.tar.gz
 tar -jxvf file.tar.bz2
 tar -xZvf file.tar.Z
 tar -zxvf test.tar.gz -C /tmp

Common Decompression Commands Summary

*.tar → tar -xvf *.gz → gzip -d or gunzip *.tar.gz and *.tgz → tar -xzf *.bz2 → bzip2 -d or bunzip2 *.tar.bz2 → tar -xjf *.Z → uncompress *.tar.Z → tar -xZf *.rar → unrar e *.zip →

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

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