Master Linux File Packaging: Zip, Tar, Gzip, Bzip2, XZ & Split Explained
Learn how to efficiently package and compress files on Linux using tools such as zip, tar, gzip, bzip2, xz and split, covering command syntax, options, practical examples, and tips for handling directories, exclusions, and cross‑platform compatibility.
1. What is file compression?
File compression combines multiple files or directories into a single archive, reducing size for easier storage and faster transmission.
2. Why compress files?
Compressed archives occupy less space (e.g., a 28 GB folder can shrink to ~6 GB) and transfer more quickly, saving bandwidth and time.
3. Windows vs. Linux archive compatibility
Windows commonly uses zip and rar; Linux prefers zip, tar.gz, and tar.bz2. Linux can open zip files, but it does not natively support Windows‑only rar archives.
4. Common Linux compression formats
.zip – created with the zip tool
.gz – created with gzip (usually combined with tar)
.bz2 – created with bzip2 (usually combined with tar)
.tar.gz – tar archive then compressed with gzip.tar.bz2 – tar archive then compressed with
bzip25. gzip
# Install gzip (if not present)</code>
<code># yum install gzip -y</code>
<code># Compress a single file</code>
<code># gzip file</code>
<code># View compressed content without extracting</code>
<code># zcat file.gz</code>
<code># Decompress</code>
<code># gzip -d file.gz</code>
<code># Example: compress all repo files</code>
<code># cd /etc/yum.repos.d/</code>
<code># gzip *Key options: -c (output to stdout, keep original), -1 … -9 (compression level, default 9), -d (decompress), zcat (view without extracting), gunzip (decompress).
6. bzip2
# Compress a file</code>
<code># bzip2 file</code>
<code># Decompress</code>
<code># bunzip2 file.bz2</code>
<code># Keep original file while compressing</code>
<code># bzip2 -k file</code>
<code># Specify compression level</code>
<code># bzip2 -1 … -9 file</code>
<code># View contents without extracting</code>
<code># bzcat file.bz27. xz
# Decompress an xz archive</code>
<code># unxz file.xz</code>
<code># Keep original file</code>
<code># xz -k file</code>
<code># Decompress</code>
<code># xz -d file.xz</code>
<code># Set compression level (1‑9)</code>
<code># xz -1 … -9 file</code>
<code># View without extracting</code>
<code># xzcat file.xz8. zip
# Install zip/unzip tools</code>
<code># yum install zip unzip -y</code>
<code># Compress a single file</code>
<code># zip filename.zip filename</code>
<code># Compress multiple files</code>
<code># zip filename1.zip file1 file2 /etc/hosts</code>
<code># Recursively compress a directory</code>
<code># zip -r dir.zip dir/</code>
<code># Verify archive integrity</code>
<code># zip -T filename.zip</code>
<code># List archive contents without extracting</code>
<code># unzip -l filename.zip</code>
<code># Test archive</code>
<code># unzip -t filename.zip</code>
<code># Extract to current directory</code>
<code># unzip filename.zip</code>
<code># Extract to a specific directory</code>
<code># unzip filename.zip -d /opt/9. tar
Tar creates archives; compression is added via options.
# Basic syntax</code>
<code># tar [-zjxcvfpP] -f archive_name files...</code>
<code># Options</code>
<code># c – create archive</code>
<code># x – extract archive</code>
<code># t – list contents</code>
<code># v – verbose output</code>
<code># f – specify file name (must be last flag)</code>
<code># z – gzip compression (produces .tar.gz)</code>
<code># j – bzip2 compression (produces .tar.bz2)</code>
<code># J – xz compression (produces .tar.xz)</code>
<code># C – change to directory before extracting</code>
<code># --exclude=pattern – exclude files/directories</code>
<code># Common combos</code>
<code># czf archive.tar.gz files… (gzip)</code>
<code># cjf archive.tar.bz2 files… (bzip2)</code>
<code># cJf archive.tar.xz files… (xz)</code>
<code># zxf archive.tar.gz (extract gzip archive)</code>
<code># jxf archive.tar.bz2 (extract bzip2 archive)</code>
<code># xf archive.tar (auto‑detect compression)</code>
<code># xvf archive.tar (verbose extract)</code>
<code># tf archive.tar (list contents)10. Packing with exclusions
# Exclude a single file</code>
<code># tar czf etc.tar.gz --exclude=etc/services etc/</code>
<code># Exclude multiple files</code>
<code># tar czf etc.tar.gz --exclude=etc/services --exclude=etc/rc.local etc/</code>
<code># Use an exclusion list file</code>
<code># cat exclude.list</code>
<code># etc/services</code>
<code># etc/rc.local</code>
<code># tar czfX etc.tar.gz exclude.list etc/11. Viewing and extracting archives
# List archive contents</code>
<code># tar tf test.tar.gz</code>
<code># Extract to current directory</code>
<code># tar xf test.tar.gz</code>
<code># Extract to a specific directory</code>
<code># tar xf test.tar.gz -C /tmp12. split
# Split a large file into 2 M chunks with numeric three‑digit suffixes</code>
<code># split -b 2M etc.tar.gz -d -a 3 etc.tar.gz</code>
<code># Reassemble</code>
<code># cat etc.tar.gz??? > combined.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.
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.)
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.
