Extract a gzip file
Decompress a `.gz` file — single-file compression you encounter in `.tar.gz`, log rotation (`access.log.gz`), and package metadata.
How to extract a gzip file in each shell
gunzip file.gzREMOVES the `.gz` file and replaces with the uncompressed version. Use `-k` (`gunzip -k file.gz`) to keep the original. `gunzip` is a symlink to `gzip -d`. `zcat file.gz` decompresses to stdout without touching the file.
gunzip file.gzgunzip file.gz$in = [System.IO.File]::OpenRead("file.gz"); $out = [System.IO.File]::Create("file"); $gz = New-Object System.IO.Compression.GZipStream $in, ([System.IO.Compression.CompressionMode]::Decompress); $gz.CopyTo($out); $gz.Close(); $out.Close(); $in.Close()pwsh has NO native `gunzip` — must use .NET `GZipStream`. For convenience, `tar.exe -xzf file.tar.gz` works on Windows 10+ for combined tar+gzip. Or install `7z` and use `7z x file.gz`.
tar.exe -xzf file.tar.gzWraps gunzip + tar in one step (works on `.tar.gz`/`.tgz`). For a bare `.gz` (single file, no tar): `powershell -NoProfile -Command "..."` with the GZipStream pattern, or install 7-Zip.
Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.
Gotchas & notes
- **`gunzip` REMOVES the source by default** — surprising on first run when you wanted to keep the `.gz` for re-use. Always use `-k` (`gunzip -k file.gz`) for "extract a copy" semantics. Equivalently: `zcat file.gz > file` (decompresses to stdout, redirect to file — leaves `.gz` untouched). Same flag exists on `gzip -kd file.gz`. The destructive default exists because gzip predates today's storage; in 1992 disks were tiny and the round-trip removal saved space. Doesn't make sense in 2026 but the default never changed.
- **`zcat` / `zless` / `zgrep` / `zdiff` operate on .gz WITHOUT extracting** — invaluable for log analysis: `zgrep "ERROR" access.log.{1..7}.gz` greps across a week of rotated logs without expanding any of them. `zcat -f file` also handles uncompressed files (passes through), making scripts that operate on mixed-state log dirs cleaner: `for f in *.log *.log.gz; do zcat -f "$f" | analyze; done`. macOS has `zcat` (BSD) which behaves slightly differently — `zcat` on macOS works on `.Z` (compress) AND `.gz`; on Linux it's gzip-only and `.Z` needs `uncompress`. Use `gunzip -c file.gz` for portable "to stdout" behaviour.
- **Pwsh GZipStream is the only pure-pwsh answer** — no `gunzip` cmdlet exists in any pwsh version. The 10-line script above is reusable as a function: `function Expand-Gzip($in, $out) { $i=[IO.File]::OpenRead($in); $o=[IO.File]::Create($out); $g=New-Object IO.Compression.GZipStream $i, "Decompress"; $g.CopyTo($o); $g.Dispose(); $o.Close(); $i.Close() }`. For PowerShell community modules, `Install-Module 7Zip4PowerShell` adds `Expand-7Zip` which handles `.gz` (and tar, zip, 7z, etc.). For ad-hoc tasks, install `7z.exe` or `gzip.exe` via Chocolatey: `choco install gzip` (Windows port of GNU gzip).
- **.gz is for SINGLE FILES** — there is no such thing as a "gzip archive with multiple files". `.tar.gz` is "tar an archive, then gzip the tar" (two steps). When you see a folder compressed to `.gz`, it's actually `.tar.gz`. Anti-pattern: `gzip dir/` gzips EACH FILE in dir/ separately (you end up with dir/file1.gz, dir/file2.gz — useless for transport). Correct: `tar czf dir.tar.gz dir/` (one archive containing everything). Same trap for `.bz2`, `.xz`, `.zst` — all are single-file compressors that pair with tar for multi-file.
- **Compression speed vs ratio** matters for large files. `gzip` levels: `-1` (fastest, ~50% ratio), `-6` (default, ~70%), `-9` (best, ~75% with much more CPU). For large logs, `-1` is often the right pick (CPU saved > extra storage). Modern alternatives: `zstd` (Facebook's zstandard — same compression as gzip-6 at 5x the speed; install via apt/brew; produces `.zst` files), `lz4` (extreme speed, lower ratio — good for real-time pipelines), `xz` (better ratio than gzip, slower — common for source tarballs). `gzip` remains the most-installed; for new pipelines, `zstd` is the cost-effective default.
Related commands
Related tasks
- Extract a tar archive— Unpack a `.tar`, `.tar.gz`, or `.tar.bz2` file into the current directory.
- 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.
- 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.
- Create a tar archive from a directory— Bundle a directory tree into a single compressed file — for backups, transport, version-snapshot, and the `dist/` artifact step in a build.