Copyright (c) 2026 MindMesh Academy. All rights reserved. This content is proprietary and may not be reproduced or distributed without permission.

2.5.1. Archiving and Compression

💡 First Principle: tar (Tape Archive) was designed to write files sequentially to tape. This lineage explains its flags: -c creates, -x extracts, -f specifies the file, -z/-j/-J add compression pipelines. Understanding the flag meanings makes them memorable.

tar — The Universal Archive Tool:
# Create archives
tar -czf backup.tar.gz /etc/          # Create gzip-compressed archive
tar -cjf backup.tar.bz2 /home/alice/  # Create bzip2-compressed archive
tar -cJf backup.tar.xz /var/www/      # Create xz-compressed archive (best ratio)
tar -cf archive.tar /data/            # Create uncompressed archive

# Extract archives
tar -xzf backup.tar.gz                # Extract gzip archive (current dir)
tar -xzf backup.tar.gz -C /restore/  # Extract to specific directory
tar -xf archive.tar specific/file     # Extract single file

# Inspect without extracting
tar -tzf backup.tar.gz                # List contents of gzip archive
tar -tvf archive.tar                  # List with verbose details

# Key flags:  -c=create  -x=extract  -t=list  -z=gzip  -j=bzip2  -J=xz
#             -f=file  -v=verbose  -C=directory  -p=preserve permissions
Compression Tools Comparison:
ToolExtensionSpeedRatioBest For
gzip.gzFastGoodGeneral use, log compression
bzip2.bz2MediumBetterSource archives
xz.xzSlowBestDistribution packages
7-Zip.7zVariableExcellentCross-platform, large files
unzip.zipFastModerateWindows compatibility
gzip file.txt                   # Compress in place (removes original)
gzip -k file.txt                # Keep original (-k = keep)
gunzip file.txt.gz              # Decompress
zcat file.gz                    # View compressed file without decompressing
zgrep "error" file.gz           # grep inside compressed file
zless file.gz                   # Page through compressed file

bzip2 file.txt
bunzip2 file.txt.bz2

xz file.txt
unxz file.txt.xz
cpio:

cpio is an older archive format still relevant for initramfs and some package management contexts. It reads filenames from stdin:

find /etc -name "*.conf" | cpio -o > configs.cpio   # Create
cpio -id < configs.cpio                               # Extract

⚠️ Exam Trap: tar with -z uses gzip, -j uses bzip2, and -J (capital J) uses xz. Getting the case wrong on -J vs -j is a common mistake. Also remember: tar extracts to the current directory by default — use -C /target/ to specify a destination.

Reflection Question: A developer gives you a file named release.tar.xz to deploy. Write the single command to extract it directly into /opt/myapp/.

Alvin Varughese
Written byAlvin Varughese
Founder18 professional certifications