LinuxCommandLibrary

7z

Compress or extract archives

TLDR

[a]dd a file or directory to a new or existing archive

$ 7z a [path/to/archive.7z] [path/to/file_or_directory]
copy

Encrypt an existing archive (including filenames)
$ 7z a [path/to/encrypted.7z] -p[password] -mhe=on [path/to/archive.7z]
copy

E[x]tract an archive preserving the original directory structure
$ 7z x [path/to/archive.7z]
copy

E[x]tract an archive to a specific directory
$ 7z x [path/to/archive.7z] -o[path/to/output]
copy

E[x]tract an archive to stdout
$ 7z x [path/to/archive.7z] -so
copy

[a]rchive using a specific archive type
$ 7z a -t[7z|bzip2|gzip|lzip|tar|zip] [path/to/archive] [path/to/file_or_directory]
copy

[l]ist the contents of an archive
$ 7z l [path/to/archive.7z]
copy

Set the level of compression (higher means more compression, but slower)
$ 7z a [path/to/archive.7z] -mx=[0|1|3|5|7|9] [path/to/file_or_directory]
copy

SYNOPSIS

7z <command> [<switches>...] <archive_name> [<file_names>...] [@listfile]

Common commands:

7z a [<switches>...] <archive_name> [<file_names>...] <directories>...

7z e [<switches>...] <archive_name> [<file_names>...]

7z x [<switches>...] <archive_name> [<file_names>...]

7z l [<switches>...] <archive_name>

7z d [<switches>...] <archive_name> [<file_names>...]

7z t [<switches>...] <archive_name>

7z u [<switches>...] <archive_name> [<file_names>...]

PARAMETERS

a
    Add files to archive.

e
    Extract files from archive (without full path, extracts to current directory or specified output directory).

x
    Extract files with full path (extracts preserving the original directory structure).

l
    List contents of archive.

d
    Delete files from archive.

t
    Test integrity of archive.

u
    Update files in archive (add new files, update modified ones).

-o{dir}
    Specify output directory for extraction. Example: -o/tmp/extracted.

-p{password}
    Set password for encryption/decryption of archives.

-m{method_params}
    Set compression method and parameters. Example: -m0=Copy (no compression), -mx9=LZMA:a=2:d=256m.

-mx{N}
    Set compression level. N can be 0 (Copy), 1 (Fastest), ..., 9 (Ultra). Default is typically 5 (Normal).

-sfx[{name}.sfx]
    Create a self-extracting archive. The executable extracts itself when run.

-v{size}[b|k|m|g]
    Create volumes (split archive) of a specified size. Example: -v10m for 10MB volumes.

-r[-|0]
    Recurse subdirectories for adding files. -r enables, -r- or -r0 disables.

-y
    Assume Yes on all queries (non-interactive mode).

-ir{file_mask}
    Include files by name or mask when archiving or extracting. Example: -ir*.txt.

-xr{file_mask}
    Exclude files by name or mask when archiving or extracting. Example: -xr*.bak.

DESCRIPTION

7z is a command-line file archiver that provides a high compression ratio. It is the Linux/Unix port of the popular 7-Zip archiver for Windows, part of the p7zip project. It primarily uses the LZMA and LZMA2 compression methods, known for their excellent efficiency, especially for large files. 7z supports its native 7z archive format, which often yields significantly smaller file sizes compared to ZIP or GZIP. Beyond 7z, it can pack and unpack ZIP, GZIP, BZIP2, TAR, and XZ archives. It also supports unpacking many other formats, including AR, CPIO, DEB, DMG, ISO, RPM, WIM, and Z. Key features include strong AES-256 encryption for 7z and ZIP formats, the ability to create self-extracting archives, and support for splitting archives into multiple volumes. 7z is a versatile tool for data compression and archiving, suitable for various use cases from backup to software distribution.

CAVEATS

7z is not always pre-installed on Linux distributions and typically requires installing the p7zip-full package. Users should be aware of the distinction between the e (extract without full path) and x (extract with full path) commands, as they affect the directory structure upon extraction. While offering excellent compression, higher compression levels (e.g., -mx9) can be very CPU and memory intensive, especially for large archives, leading to significantly slower operations for both compression and decompression. The 7z format, while efficient, may not be as universally supported as ZIP or TAR.GZ on systems without 7z tools installed, which might require recipients to install specific software.

LICENSING

The majority of the 7z (p7zip) source code is distributed under the GNU LGPL license. However, the unRAR code, which allows 7z to unpack RAR archives, is under a different, more restrictive license, which might affect commercial redistribution of compiled binaries in some contexts.

SELF-EXTRACTING ARCHIVES (SFX)

The -sfx switch allows the creation of self-extracting archives. These are executable files (typically .exe on Windows, or a shell script on Linux) that contain the archived data and an integrated decompression engine. This means the recipient does not need to have 7z or any other archiving software installed to extract the contents, making distribution simpler.

ENCRYPTION

7z provides strong encryption capabilities, supporting AES-256 for both 7z and ZIP formats. This allows users to protect sensitive data within archives using a password, ensuring confidentiality during storage or transmission. It's important to remember a forgotten password will render the encrypted archive irrecoverable.

HISTORY

The 7z command is the command-line interface for 7-Zip, a file archiver originally developed by Igor Pavlov. The first public release of 7-Zip for Windows was in 1999. The Linux/Unix port, known as p7zip, brought the advanced compression capabilities of 7-Zip to these platforms. Its development was primarily driven by the introduction and refinement of the LZMA (Lempel-Ziv-Markov chain Algorithm) compression method, which offers exceptionally high compression ratios, often outperforming older methods like Deflate (used by ZIP and GZIP). Over the years, 7z has gained significant popularity in the Linux community for its efficiency, versatility, and open-source nature, making it a preferred choice for archival tasks requiring maximum compression.

SEE ALSO

zip(1), unzip(1), tar(1), gzip(1), bzip2(1), xz(1), unar(1)

Copied to clipboard