gzip — Compress or decompress a single file with DEFLATE in place across all 5 shells
Equivalents in every shell
Bashunix
gzip fileZshunix
gzip fileFishunix
gzip filePowerShellwindows
tar -czf file.gz fileNo native single-file gzip. For pure gzip of one file, use `[System.IO.Compression.GZipStream]` or install GNU gzip.
cmd.exewindows
tar -czf file.tar.gz filecmd has no native gzip. `tar` produces a gzipped tarball, not a bare `.gz`. Use 7-Zip CLI for single-file gzip.
Worked examples
Compress a single file in place (replaces `file` with `file.gz`)
Bash
gzip access.logPowerShell
$in = [IO.File]::OpenRead("access.log"); $out = [IO.File]::Create("access.log.gz"); $gz = [IO.Compression.GZipStream]::new($out, "Compress"); $in.CopyTo($gz); $gz.Close(); $in.Close()Decompress a .gz file
Bash
gunzip access.log.gzPowerShell
$in = [IO.File]::OpenRead("access.log.gz"); $out = [IO.File]::Create("access.log"); $gz = [IO.Compression.GZipStream]::new($in, "Decompress"); $gz.CopyTo($out); $gz.Close(); $out.Close()Pipe through gzip to stream-compress
Bash
cat huge.sql | gzip > huge.sql.gzPowerShell
# No idiomatic one-liner. Use `tar -czf out.tar.gz huge.sql` or invoke GNU gzip via WSL / Git Bash.Gotchas
- gzip removes the original file by default — pass `-k` (or `--keep`) to keep it.
- gzip handles ONE file at a time. To bundle a directory, combine with tar: `tar -czf out.tar.gz dir/`.
- PowerShell ships `GZipStream` but no convenient cmdlet wrapper — the one-liners are verbose. If you do this often, write a helper function or install GNU gzip.
- gzip-compressed text is widely supported by web servers (`Content-Encoding: gzip`); browsers handle decompression transparently.
WSL & PowerShell Core notes
pwshpwsh has no `gzip` cmdlet on any platform. The .NET `[IO.Compression.GZipStream]` class is available everywhere but the inline one-liners are 4–5 lines — for single-file gzip work, install GNU gzip or shell out (`/usr/bin/gzip` on Linux/macOS pwsh hosts, `tar -czf out.tar.gz file` on Windows for tarballs only since `tar` cannot produce a bare `.gz`). Throughput-wise `[IO.Compression.GZipStream]` is competitive with the GNU binary — the cost is verbosity, not speed.
WSLGNU `gzip` ships in every WSL distro. Gzipping a file on `/mnt/c/...` works but is bandwidth-bound by DrvFs reads (~150–250 MB/s typical). One round-trip pitfall: `gzip -d windows.txt.gz` decompresses to `windows.txt` with the WSL user as owner and mode `0644` — Windows ACLs on the original file are NOT preserved through `.gz` (the format only stores name, mtime, and a single byte of OS hint). For Windows-native consumers that need to keep ACLs, prefer `tar -czf` with `--xattrs` or `Compress-Archive` from the Windows side.
Common tasks using gzip
- Compress files into a zip archive
Bundle a folder or set of files into a single `.zip` archive.
- 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.
- Extract a gzip file
Decompress a `.gz` file — single-file compression you encounter in `.tar.gz`, log rotation (`access.log.gz`), and package metadata.
- Extract a tar archive
Unpack a `.tar`, `.tar.gz`, or `.tar.bz2` file into the current directory.