Operations 10 min read

Master Linux File Compression: zip, tar, gzip, bzip2 & split

This guide explains why and how to compress files on Linux, compares Windows and Linux archive formats, lists common compression types, and provides practical command‑line examples for zip, gzip, bzip2, xz, tar and split utilities.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux File Compression: zip, tar, gzip, bzip2 & split

1. File Packaging and Compression

1. What is file compression?

Combining multiple files or directories into a single archive, similar to packing belongings when moving.

2. Why compress files?

Compressed files are smaller, transfer faster, and save network bandwidth; for example, a 28 GB folder can be reduced to about 6 GB.

3. Compatibility between Windows and Linux archives

Windows commonly uses zip and rar; Linux commonly uses zip and tar.gz. Linux archives can be opened on Windows, but Linux does not support Windows RAR format. Zip is the usual cross‑platform format.

4. Common compression formats on Linux

Format

Tool

.zip

zip

.gz

gzip (usually used with tar)

.bz2

bzip2 (usually used with tar)

.tar.gz

tar + gzip

.tar.bz2

tar + bzip2

2. gzip packaging and compression

Using gzip to compress files.

# Only works on files, not directories; compression deletes the original file, extraction deletes the archive
yum install gzip -y
gzip file               # compress a file
zcat file.gz            # view compressed file without extracting
gzip -d file.gz         # decompress
# Example: quickly disable/enable configuration files
cd /etc/yum.repos.d/
 gzip *
 gzip CentOS-Vault.repo   # creates CentOS-Vault.repo.gz
zcat CentOS-Vault.repo.gz # view without extracting

Common options:

gzip [option]... file
-c   output to screen, keep original file
-1‑9 compression level (default 9)
-d   decompress
zcat view without decompressing
gunzip decompress

3. bzip2

bzip2 usage:

bzip2 [option] file
bunzip2               # decompress
-k   keep original file
-d   decompress
-1‑9 compression level
bzcat view without decompressing

4. xz

xz usage:

unxz                 # decompress
-k   keep original file
-d   decompress
-1‑9 compression level
xzcat view without decompressing

5. zip packaging and compression

zip compresses files; unzip extracts them.

# Install zip/unzip
yum install zip unzip -y
# Compress a single file
zip filename.zip filename
# Compress multiple files
zip filename1.zip file1 file2 /etc/hosts
# Compress a directory recursively
zip -r dir.zip dir/
# Test archive integrity
zip -T filename.zip
# List contents without extracting
unzip -l filename.zip
unzip -t filename.zip
# Extract to current directory
unzip filename.zip
# Extract to a specific directory
unzip filename.zip -d /opt/

6. tar packaging and compression

tar is the most common Linux archiving tool; it can compress with gzip, bzip2, or xz.

tar [-zjxcvfpP] filename
c   create archive
x   extract archive
t   list archive contents
v   verbose output
f   specify archive file name
z   gzip compression (.tar.gz)
j   bzip2 compression (.tar.bz2)
J   xz compression (.tar.xz)
C   specify extraction directory
--exclude   exclude files or directories
# Common combinations
czf   create .tar.gz
cjf   create .tar.bz2
cJf   create .tar.xz
zxf   extract .tar.gz
jxf   extract .tar.bz2
xf    auto‑detect compression
xvf   extract with verbose output
tf    list archive contents

Examples:

# Archive /etc directory
tar cpvf etc.tar /etc
# Append files to an existing archive
tar -r -f etc.tar /etc
# View archive contents
tar -t -f etc.tar
# Extract archive
tar xf etc.tar
# Extract to /opt
tar xf etc.tar -C /opt/
# Create compressed archives
tar czf etc.tar.gz /etc          # gzip
tar cjf etc.tar.bz2 /etc        # bzip2
tar Jcf etc.tar.xz --exclude /etc/sysconfig /etc   # xz with exclusion

7. split

Split large files into smaller pieces.

split -b 2M etc.tar.gz -d -a 3   # split into 2 MB parts, numeric suffix with 3 digits
cat xx* > etc.tar.gz            # reassemble pieces
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.

GzipFile Compressionzipcommand-linetar
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.