Chapter 9: Archives and Compression tools in Linux

Archives and Compression in Linux

Working with compressed files is essential for saving disk space and transferring data efficiently. This chapter covers the most common archiving and compression tools in Linux.


1. tar – Tape Archive (Basic Archiving)

The standard tool for combining multiple files into one archive (.tar).

Create an Archive

# -c = create, -v = verbose, -f = filename
tar -cvf archive_name.tar file1 file2 dir1/

Extract an Archive

# -x = extract
tar -xvf archive_name.tar

List Contents

# -t = list
tar -tvf archive_name.tar

2. gzip/gunzip – Compression (.gz)

Compresses files (but doesn’t bundle multiple files like tar).

Compress a File

# Creates file.txt.gz
gzip file.txt

Decompress

# Restores file.txt
gunzip file.txt.gz

tar + gzip Together (.tar.gz)

# Create compressed archive
tar -czvf archive.tar.gz files/

# Extract
tar -xzvf archive.tar.gz

3. zip/unzip – Cross-Platform Compression (.zip)

Commonly used for Windows-compatible archives.

Create a ZIP Archive

# Include files/dirs
zip archive.zip file1.txt dir1/

Extract ZIP

unzip archive.zip

List Contents

unzip -l archive.zip

4. xz – High-Ratio Compression (.xz)

Slower but better compression than gzip.

Compress a File

# Creates file.txt.xz
xz file.txt

Decompress

# Or: xz -d file.txt.xz
unxz file.txt.xz

tar + xz (.tar.xz)

# Create
tar -cJvf archive.tar.xz files/

# Extract
tar -xJvf archive.tar.xz

-J Compress using xz. xz is a compression format and a compression tool, similar to gzip or bzip2, but with better compression ratios (i.e., smaller file sizes), especially for large files.


5. 7z – Ultra-High Compression (.7z)

Even better compression (requires p7zip package).

Install (if missing)

# Debian/Ubuntu
sudo apt install p7zip-full

# RHEL/CentOS
sudo yum install p7zip

Create Archive

# a = add
7z a archive.7z file1 dir1/

Extract

# a = add
7z a archive.7z file1 dir1/

Compression Types and features

FormatUse CaseCompressionPlatform Notes
.tarUncompressed bundlingNoneNative to Unix/Linux
.tar.gzBest balance (speed + compression)ModerateVery common, fast
.zipSharing with Windows usersModerateCross-platform, widely supported
.tar.xzMaximum compression (slow)HighSmaller size, slower to create
.7zExtreme compressionVery HighRequires p7zip or 7-Zip

Note that .tar is just a container it is not compressing the contents.

Summary Cheat Sheet

CommandAction / Output
tar -cvf file.tar files/Create .tar (uncompressed)
tar -xvf file.tarExtract .tar
gzip fileCreate .gz
gunzip file.gzExtract .gz
tar -czvf file.tar.gz files/Create .tar.gz (gzip)
tar -xzvf file.tar.gzExtract .tar.gz
xz fileCreate .xz
unxz file.xzExtract .xz
tar -cJvf file.tar.xz files/Create .tar.xz (xz)
tar -xJvf file.tar.xzExtract .tar.xz
zip archive.zip files/Create .zip
unzip archive.zipExtract .zip
7z a archive.7z files/Create .7z (7-Zip)
7z x archive.7zExtract .7z