7z

Learn how to use the 7z command line tool for high-ratio file compression and archiving. Explore essential commands like add, delete, extract, and list with practical examples.

7z Command Line Archiver

The 7z command line tool is a powerful utility for file archiving and compression, known for achieving the highest compression ratios. This guide covers its essential commands and provides practical examples for efficient file management.

7z Command Usage

The basic syntax for the 7z command is:

7z [Args] [archive.7z] [files / folders to archive]

Core 7z Arguments

  • a: Add files to an archive.
  • d: Delete files from an archive.
  • e: Extract files from an archive without directory structure.
  • l: List the contents of an archive.
  • t: Test the integrity of an archive.
  • u: Update existing files in an archive.
  • x: Extract files with their full paths.

Important Considerations for Linux/Unix Users

It is crucial to note that the 7-zip format does not store file owner/group information. For backup purposes on Linux/Unix systems, it is recommended to use tar in conjunction with 7z:

  • To backup a directory: tar cf - directory | 7z a -si directory.tar.7z
  • To restore your backup: 7z x -so directory.tar.7z | tar xf -

If you need to transfer files and directories to other Unix/MacOS/Windows users without preserving owner information, the 7-zip format is suitable.

7z Compression Examples

Example 1: Quick and Easy Archive Creation

Add all files in dir1 to archive.7z using default compression settings:

7z a archive.7z  dir1

Example 2: Maximum Compression with LZMA

Add all files in dir1 to archive.7z with ultra compression using the LZMA method:

7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on archive.7z dir1

Explanation of options:

  • -t7z: Specifies the 7z archive format.
  • -m0=lzma: Selects the LZMA compression method.
  • -mx=9: Sets the compression level to 9 (ultra).
  • -mfb=64: Configures the number of fast bytes for LZMA to 64.
  • -md=32m: Sets the dictionary size to 32 MB.
  • -ms=on: Enables solid archiving, which can improve compression ratios for many small files.

Example 3: Storing Files with No Compression

Add all files in dir1 to archive.7z without any compression (storing only):

7z a -m0=copy archive.7z dir1

7z Exit Codes

Understanding 7z exit codes can help in scripting and error handling:

  • 0: Normal operation, no errors or warnings.
  • 1: Warning, non-fatal errors occurred.
  • 2: Fatal error encountered.
  • 7: Invalid command-line arguments provided.
  • 8: Insufficient memory for the operation.
  • 255: The process was interrupted by the user.

External Resources