The 7z command in Linux: compressor with support for multiple formats

Introduction to the 7z command in Linux

The 7z command belongs to the p7zip package and offers a powerful compression and decompression tool that supports a wide variety of formats, including its own .7z format, ZIP, GZIP, BZIP2, TAR, XZ, and many more. Its LZMA and LZMA2 algorithm provides compression ratios superior to those of traditional tools, making it ideal for saving disk space and speeding up transfers.

Installation on popular distributions

In most Linux distributions, p7zip is available in the official repositories. To install it on Ubuntu or Debian, run:

  • sudo apt update && sudo apt install p7zip-full

On Fedora:

  • sudo dnf install p7zip

On Arch Linux:

  • sudo pacman -S p7zip

After installation, the 7z command will be available in any terminal.

Basic syntax

The general form of the command is:

7z [commands] [options] [files...]

The most used commands are a (add/compress), x (extract), and l (list contents). Options allow adjusting compression level, dictionary size, solidification, and encryption.

Compression examples

To create a .7z file with maximum compression:

  • 7z a -t7z -mx=9 archive.7z folder/

The -t7z modifier specifies the format, while -mx=9 sets the compression level (from 0 to 9). If you want to create a ZIP compatible with most systems:

  • 7z a -tzip archive.zip folder/

You can compress several directories or files simply by listing them after the archive name.

Extracting files

To decompress a file while preserving the directory structure:

  • 7z x archive.7z

If you prefer to extract to a specific directory:

  • 7z x archive.7z -o/path/to/destination

The command also allows extracting only certain file types using wildcards:

  • 7z x archive.7z '*.pdf'

Supported formats

7z recognizes and can handle more than 30 different formats. Some of the most relevant are:

  • 7z (LZMA/LZMA2)
  • ZIP, ZIP64
  • GZIP, BZIP2, XZ
  • TAR, CPIO, ISO
  • ARJ, CAB, CHM, DMG, HFS, LZH, NSIS, RAR, RPM, UDF, WIM, Z

Advanced options

To adjust compression according to your needs, you can modify the dictionary size:

  • 7z a -t7z -md=32m archive.7z folder/

A larger dictionary improves compression for large files but consumes more RAM. Solidification, enabled by default in the 7z format, treats several files as a continuous stream, improving the ratio when there is much redundancy among them. You can disable it with -ms=off if you need fast random access.

AES‑256 encryption is added with:

  • 7z a -t7z -p'my_password' -mem=AES256 archive.7z folder/

Remember that the password appears in the shell history; for greater security, use -p without a value and you will be prompted interactively.

Comparison with other tools

Although gzip and bzip2 are fast and simple, their compression ratio is inferior to that of 7z

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .