Extract a 7z archive
Unpack a `.7z` file — the highest-ratio common archive format, popular on Windows software downloads and big game/asset bundles.
How to extract a 7z archive in each shell
7z x archive.7z`x` extracts WITH full paths (preserves directory structure). `e` extracts FLATTENED (all files into target dir, no subdirs). `-o./target` sets output dir (no space after `-o`). `-ppass` for password (no space). `7z` is from `p7zip` package — install `apt install p7zip-full` / `brew install p7zip`.
7z x archive.7z7z x archive.7zExpand-7Zip archive.7z -TargetPath .Requires the `7Zip4PowerShell` module (`Install-Module 7Zip4PowerShell -Scope CurrentUser`). Native pwsh has NO 7z support. Alternative: `7z.exe x archive.7z` if 7-Zip is installed (`choco install 7zip` / `winget install 7zip`).
"C:\Program Files\7-Zip\7z.exe" x archive.7zcmd has NO native 7z support. Install 7-Zip (`winget install 7zip`) and use `7z.exe`. The full path is needed unless `C:\Program Files\7-Zip` is in PATH (the installer offers this option).
Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.
Gotchas & notes
- **`7z x` vs `7z e` is the #1 source of "where did my files go" confusion**: `x` (eXtract with full paths) recreates the directory structure inside the archive. `e` (Extract flattened) dumps every file into the target directory, IGNORING subdirectories — files with the same name in different subdirectories collide and overwrite. ALWAYS use `x` unless you specifically want flattening. The semantics inherit from PKZIP-era; modern `7z` shows a warning when `e` would cause collisions, but older versions silently overwrite.
- **`7zz` is the modern fork** of `7z` for Linux/macOS. The classic `p7zip` (Linux port) has been UNMAINTAINED since 2016 (last upstream release). `7zz` is built from the modern Windows 7-Zip source by Igor Pavlov himself, released starting 7-Zip 21.00 (2021). Same flags, but: handles modern formats (Zstandard, Brotli), faster on multi-core, fixes CVEs that p7zip never patched. Install: `brew install sevenzip` (provides `7zz`), or download the official Linux binary from 7-zip.org. For new scripts, prefer `7zz` over `7z` where available.
- **Encryption modes**: 7z supports TWO encryption modes — "encrypt only the file contents" (default) and "encrypt headers" (file LIST is also encrypted, so `7z l archive.7z` requires the password). The header-encrypted form is the genuinely-secure one — without password you don't even know what's inside. Create with `7z a -ppass -mhe archive.7z dir/` (`-mhe` = methodHeaderEncryption). For high-value archives (legal docs, financial records, source-code snapshots), `-mhe` should be the default. Standard AES-256 (`-mhe=on` implies it); password-stretching uses 2^19 SHA-256 iterations (resistant to brute force unless password is weak).
- **Listing without extracting**: `7z l archive.7z` lists entries (without `-slt` shows the standard table; `-slt` "show long types" gives full per-entry detail including compression method, CRC, attributes). `7z l -ba archive.7z` ("bare" — only the file list, no header / footer). For scripting: `7z l -ba -slt archive.7z | awk '/^Path = / {print $3}'` extracts just paths. For very large archives, listing is much cheaper than extracting — always preview before unpacking.
- **7z's killer use case is high-compression on large repetitive data** — VM images, log archives, source code with many similar files. Solid mode (`-ms=on`, default): treats all files as one giant block for compression, achieves much better ratios on repetitive content (often 2-4x smaller than tar.gz) but extracting ANY single file requires decompressing up to that file from the start. For random-access friendliness, use `-ms=off` (non-solid). Anti-pattern: a 100 GB solid 7z archive containing 1 million files — extracting a single named file takes the full decompression time. Trade ratio for access pattern based on use case.
Related commands
Related tasks
- Extract a zip archive— Unpack a `.zip` file from the command line — the most common archive format on the web, and a frequent source of "how do I unzip on Windows without GUI" searches.
- Extract a tar archive— Unpack a `.tar`, `.tar.gz`, or `.tar.bz2` file into the current directory.
- Compress files into a zip archive— Bundle a folder or set of files into a single `.zip` archive.
- List the contents of a tar archive— Show what's inside a `.tar`, `.tar.gz`, `.tar.xz`, etc. without extracting — for sanity-checking before unpacking, for grep-searching member names, and for size auditing.