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

Introduction to the 7z command in Linux

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

Installation in popular distributions

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

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

In Fedora:

  • sudo dnf install p7zip

In Arch Linux:

  • sudo pacman -S p7zip

After installation, the command7zwill be available at any terminal.

Basic syntax

The general form of the command is:

7z [comandos] [opciones] [archivos...]

The most used commands area(add / compress),x(extract) andl(list content). The options allow to adjust the compression level, dictionary size, solidification and encryption.

Examples of compression

To create a .7z file with maximum compression:

  • 7z a -t7z -mx=9 archivo.7z carpeta/

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

  • 7z a -tzip archivo.zip carpeta/

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

File extraction

To decompress a file while maintaining the directory structure:

  • 7z x archivo.7z

If you prefer to extract to a specific directory:

  • 7z x archivo.7z -o/ruta/destino

The command also allows to extract only certain types of files using comodines:

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

Formats supported

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 the compression according to your needs, you can change the size of the dictionary:

  • 7z a -t7z -md=32m archivo.7z carpeta/

A larger dictionary improves compression in large files, but consumes more RAM. The solidification, activated by default in 7z format, treats several files as a continuous flow, improving the ratio when there is much redundancy between them. You can deactivate it with-ms=offIf you need quick random access.

The AES-256 encryption is added with:

  • 7z a -t7z -p'mi_contraseña' -mem=AES256 archivo.7z carpeta/

Remember that the password is shown in the shell history; for more security, use-pno value and you will be asked in an interactive way.

Comparison with other tools

Althoughgzipandbzip2are fast and simple, their compression ratio is lower than 7z in files with repetitive patterns.xzoffers 7z-like ratios, but lacks the wide multi-format support and the unified interface that p7zip provides. In scenarios where maximum compatibility is needed, ZIP remains the most portable option, although 7z can create ZIP files with higher compression levels than those ofzipstandard.

Tips and problem solving

  • Verify the installed version with7zwithout arguments; shows license and version information.
  • If you get memory errors when using a large dictionary, it reduces its size with-mdor close other applications.
  • To compress very large files, consider using the volume mode:7z a -v100m archivo.7z carpeta/creates parts of 100 MB.
  • Check the record of operations with7z l -slt archivo.7zto see details of each compressed file.

Conclusion

The command7zis a versatile and powerful solution for any Linux user who needs to compress or decompress files in multiple formats. Its high efficiency, wide support and configuration options make it a superior alternative to classic tools, especially when it is sought to maximize space saving without sacrificing flexibility. Install p7zip and become familiar with your syntax will allow you to handle almost any file task from the terminal with confidence.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish