Master Linux File Compression: zip, gzip, bzip2, xz, tar & split
This guide explains the concepts and practical commands for file packaging and compression on Linux, covering zip, gzip, bzip2, xz, tar, and split utilities, their options, usage examples, and cross‑platform compatibility with Windows formats.
1. File Packaging and Compression
1. What is file compression?
Combine multiple files or directories into a single special file, similar to packing belongings when moving.
2. Why compress files?
When transferring large amounts of data, compression reduces size and speeds up transfer. For example, a 28 GB folder can be reduced to about 6 GB after compression. Transferring a single compressed file is faster and consumes less network bandwidth than many separate files.
3. Can Windows and Linux compression packages interoperate?
Windows commonly uses rar or zip ; Linux commonly uses zip and tar.gz . Linux formats can be opened on Windows, but Linux does not support Windows rar files. For cross‑platform compatibility, zip is usually chosen.
4. Common Linux compression types
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
Use gzip to compress files.
# Only works on files, not directories; compression deletes the original file, and the archive is removed after extraction
# yum install gzip -y
# gzip file # compress a file
# zcat file.gz # view compressed file without extracting
# gzip -d file.gz # decompress
# Example: compress all repo files
# cd /etc/yum.repos.d/
# gzip *gzip options
gzip [option]... file
-c Output result to screen, keep original file
-1‑9 Set compression level (default 9)
-d Decompress
zcat View compressed content without decompressing
gunzip Decompress3. bzip2
Compress and decompress using bzip2 .
bzip2 [option] file # compress
bunzip2 # decompress
-k Keep original file
-d Decompress
-1‑9 Set compression level
bzcat View content without decompressing4. xz
Compress and decompress using xz .
unxz # decompress
-k Keep original file
-d Decompress
-1‑9 Set compression level
xzcat View content without decompressing5. zip packaging and compression
Use zip to compress files and unzip to extract.
# Install tools if missing
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 (use -r for recursive)
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 create archives with or without compression.
Default tar creates an uncompressed archive.
# Syntax: tar [-zjxcvfpP] filename
c Create new archive
x Extract archive
t List archive contents
v Verbose output
f Specify archive file name (must be last option)
z Use gzip compression (.tar.gz)
j Use bzip2 compression (.tar.bz2)
J Use xz compression (.tar.xz)
C Specify extraction directory
X Exclude files/directories
h Archive symbolic links
--hard-dereference Archive hard links
--exclude pattern Exclude matching files/dirs
# Common combos
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 Verbose extract
tf List archive contentsExamples
# Archive a directory
tar cpvf etc.tar /etc
# Append to existing archive
tar -r -f etc.tar /etc
# List archive contents
tar tf etc.tar
# Extract archive
tar xf etc.tar
# Extract to specific directory
tar xf etc.tar -C /opt/
# Create compressed archives
tar zcf etc.tar.gz /etc # gzip
tar cjf etc.tar.bz2 /etc # bzip2
tar Jcf etc.tar.xz /etc --exclude /etc/sysconfig
# Exclude specific files while archiving
tar czf etc.tar.gz --exclude=etc/services etc/
# Exclude multiple files
tar czf etc.tar.gz --exclude=etc/services --exclude=etc/rc.local etc/
# Use an exclude list file
cat exclude.list # contains paths to exclude
tar czfX etc.tar.gz exclude.list etc/7. split
Split large files into smaller parts.
# Split a file into 2 M pieces, numeric suffix of 3 digits
split -b 2M -d -a 3 etc.tar.gz etc.part.
# Recombine parts
cat etc.part.* > etc.tar.gzSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
