ITcompressor IT — compressed and to the point
← Back to articles

Working with archives from the console

Formats fall into three groups: containers (tar) — only bundle files together, compressors (gzip, bzip2, xz) — compress a single file, archivers (zip, rar, 7z) — both bundle and compress.

Format Extension Package
ZIP .zip zip, unzip
RAR .rar rar, unrar (non-free)
TAR .tar preinstalled
GZIP .gz preinstalled
BZIP2 .bz2 preinstalled
XZ .xz preinstalled
7Z .7z p7zip-full

All the examples below revolve around a single directory:

project/
├── app.py
└── data/
    └── customers.csv

ZIP archives#

The zip and unzip packages are needed (on Debian-like systems: apt install zip unzip).

# Archive a whole directory
zip -r project.zip project/

# Add a file to an existing archive
zip project.zip notes.txt

# Unpack into the current directory
unzip project.zip

# Unpack into a specific directory
unzip project.zip -d ~/restore
# View contents without unpacking
unzip -l project.zip

# Create with a password (asks interactively)
zip -e -r project.zip project/

# Unpack a protected archive — the password is asked here
unzip project.zip

Inside a ZIP each file is compressed individually — handy for adding or pulling out single files, but on a whole directory ZIP loses to tar.gz and 7z.

RAR archives#

A proprietary format: creating requires the rar package, while the free unrar is enough for unpacking (on Debian-like systems both live in non-free).

# Archive a directory (a = add)
rar a project.rar project/

# With a password (asks interactively)
rar a -p project.rar project/

# A multi-volume archive, 100 MB each
rar a -v100m project.rar project/
# Unpack preserving the directory structure
unrar x project.rar

# Dump everything into the current folder; paths are discarded
unrar e project.rar

The rar and unrar commands are symmetric: rar x project.rar and rar e project.rar work the same way. The only difference between x/e is whether paths are preserved.

TAR containers#

tar does not compress by itself — it glues files into a single stream. Compression is enabled with a filter flag: z — gzip, j — bzip2, J — xz.

Flag What it does
c create an archive
x extract
t list contents
z gzip filter (.tar.gz)
j bzip2 filter (.tar.bz2)
J xz filter (.tar.xz)
v verbose output
f then comes the archive file name
-C change directory
# Create a compressed archive of a directory (gzip)
tar -czf project.tar.gz project/

# The same with xz: stronger compression, slower process
tar -cJf project.tar.xz project/

# View contents without unpacking
tar -tzf project.tar.gz

# Unpack into the current directory
tar -xzf project.tar.gz

# Unpack into a specific directory
tar -xzf project.tar.gz -C ~/restore

GNU tar guesses the filter from the extension: tar -xf project.tar.xz works the same as -xzf/-xjf. The old syntax without a dash (tar zxvf …) still works, but the modern GNU style uses a dash.

GZ archives#

gzip compresses a single file: app-2026-06-30.log becomes app-2026-06-30.log.gz, and the original is deleted by default.

# Compress a log (the original will be removed)
gzip app-2026-06-30.log

# Compress, keeping the original (-k = keep)
gzip -k app-2026-06-30.log

# Maximum compression (1..9, default 6)
gzip -9 app-2026-06-30.log

# Unpack (= gzip -d)
gunzip app-2026-06-30.log.gz

Compressed logs can be read and grepped on the fly, without unpacking:

zcat app-2026-06-30.log.gz          # print to stdout
zless app-2026-06-30.log.gz         # page by page
zgrep ERROR app-2026-06-30.log.gz   # search inside the compressed file

A directory cannot be compressed this way — first bundle it into a tar: tar -czf project.tar.gz project/.

XZ archives#

Works like gzip but compresses stronger and slower — the standard for distribution images and packages.

# Compress an image
xz raspi-lite.img

# Compress, keeping the original
xz -k raspi-lite.img

# Maximum level (0..9; 9e — aggressive memory for the dictionary)
xz -9e raspi-lite.img

# Unpack (= xz -d)
unxz raspi-lite.img.xz

.tar.xz is unpacked like any tar archive: tar -xJf project.tar.xz or universally tar -xf project.tar.xz.

7z archives#

The format with the best compression and support for foreign formats. The p7zip-full package is needed (on Debian-like systems: apt install p7zip-full).

# Archive a directory
7z a project.7z project/

# View contents
7z l project.7z

# Unpack into the current directory
7z x project.7z

# Unpack into a specific directory (the path goes right after -o, no space)
7z x project.7z -o$HOME/restore

# With a password (asks interactively)
7z a -p project.7z project/

7z x opens foreign formats too: the same call will unpack project.zip, project.tar.gz or project.rar.

A useful combo — the 7z + tar pipeline for on-the-fly unpacking:

7z x -so project.tar.7z | tar xf - -C ~/restore
← Back to articles