Andrew Mercer
on this page

7-Zip

Format: .7z · Algorithm: LZMA2 by default (also LZMA, PPMd, BZip2, Deflate) · First released: 1999 (Igor Pavlov)

7-Zip's .7z format gives strong ratios, supports solid compression, and has proper AES-256 encryption that can also hide filenames. The 7z command line also creates zip, tar, gzip, bzip2, xz and wim, and reads many more formats (iso, rar, cab, msi, dmg, vhd, …), which makes it a handy universal extractor.

The LZMA algorithm it introduced is what xz and lzip use.

Installing

Package names vary by distro and release:

Package Binary Notes
7zip 7zz (sometimes also 7z) Official upstream Linux build; on newer Debian, Ubuntu and Fedora
p7zip / p7zip-full (+ p7zip-plugins) 7z, 7za Older community port

The commands below use 7z; substitute 7zz if that's what you have.

Creating archives

7z a archive.7z src_dir/                  # add / create
7z a -mx=9 archive.7z src_dir/            # max compression (0-9, default 5)
7z a -mx=0 archive.7z media/              # store only
7z a -mmt=on archive.7z src_dir/          # multithreaded (often default)
7z a -ms=off archive.7z src_dir/          # non-solid: faster random access
7z a -xr'!*.log' archive.7z src_dir/      # exclude recursively
7z a -v100m archive.7z big_dir/           # split into 100 MB volumes
7z a -tzip archive.zip src_dir/           # create a zip instead
7z a -ttar archive.tar src_dir/           # or a tar

Extracting and inspecting

7z x archive.7z                           # extract with full paths
7z x archive.7z -o/tmp/dest               # output dir (no space after -o)
7z e archive.7z                           # extract flat (ignores paths)
7z x archive.7z.001                       # extract a split set
7z x archive.7z '*.conf' -r               # only matching files
7z l archive.7z                           # list
7z l -slt archive.7z                      # technical listing (methods, CRCs)
7z t archive.7z                           # test

x keeps paths; e dumps everything into one directory. You almost always want x.

Encryption

# AES-256, prompt for password, also encrypt headers (filenames)
7z a -p -mhe=on secret.7z src_dir/

# AES-256 zip (opens in 7-Zip, WinZip, modern Windows/macOS tools; not old unzip)
7z a -tzip -p -mem=AES256 secret.zip src_dir/

Without -mhe=on, anyone can list the filenames in a .7z; with it, even listing needs the password. Zip has no header encryption, so filenames always show.

Avoid -pPASSWORD on the command line in shared environments — it ends up in shell history and is visible in ps. Plain -p prompts.

Unix metadata caveat

.7z doesn't reliably preserve Unix owners, groups, some permission bits, or special files. For Linux backups, archive with tar and let 7z compress and encrypt the stream:

tar cf - /srv/data | 7z a -si -p -mhe=on data.tar.7z
7z x -so data.tar.7z | tar xf -

-si reads from stdin; -so writes to stdout.

Useful switches

Switch Meaning
-mx=N Level 0–9
-m0=lzma2 / -m0=PPMd Method (PPMd is excellent on plain text)
-md=64m Dictionary size
-ms=on\|off Solid mode
-mmt=N Threads
-p Password (prompts)
-mhe=on Encrypt headers (7z only)
-v100m Volume size
-y Assume yes to prompts
-bb1 Show filenames while working

See also