| title | Linux Command Tutorial: gzip | ||||
|---|---|---|---|---|---|
| date | 2026-09-12 00:00:00 +0000 | ||||
| categories |
|
||||
| tags |
|
||||
| draft | false | ||||
| slug | linux-gzip-tutorial | ||||
| description | Authoritative reference tutorial for gzip (GNU gzip), detailing DEFLATE algorithm compression, archive decompression, compression level tuning, integrity testing, and stream pipelines. | ||||
| upstream_suite | gnu-gzip | ||||
| upstream_version | GNU gzip 1.13 | ||||
| posix_standard | None | ||||
| research_date | 2026-09-12 |
The Linux Command Tutorial series provides rigorous, upstream-verified references for essential system commands across Linux distributions and UNIX-like environments. Each article focuses on a single executable, combining exhaustive option documentation, verified real-world examples, security boundaries, and best practices directly derived from official source documentation and POSIX standards.
Upstream: gnu-gzip (GNU gzip 1.13) | POSIX: None (De-facto Standard / RFC 1952) | Safety Tier: unprivileged-filesystem-write | Scope: stream-compression
gzip (GNU zip) is a standard file compression and decompression utility utilizing the DEFLATE algorithm (a combination of LZ77 and Huffman coding). It reduces the size of individual files, replaces original files in-place with .gz archives, and preserves file timestamps, permissions, and ownership.
- Upstream Project & Provenance: Maintained by the GNU Project under GNU gzip (
gzip), originally developed by Jean-loup Gailly and Mark Adler. - Portability & Standards Baseline: De-facto standard compression format governed by RFC 1952 (GZIP file format specification) and RFC 1951 (DEFLATE); not standardized in IEEE Std 1003.1-2024 (POSIX.1-2024).
- Target Research Implementation: Audited against GNU gzip 1.13 (
gzip(1)). - Applicability & Lifecycle: The ubiquitous compression standard for Linux distribution packages, tarballs (
.tar.gz), HTTP content encoding (Content-Encoding: gzip), and log rotation (logrotate).
gzip [options] [file...]A critical behavioral characteristic of gzip is in-place file replacement:
- Compression: Executing
gzip file.txtcreatesfile.txt.gzand deletes the originalfile.txtupon successful compression. - Decompression: Executing
gzip -d file.txt.gzrestoresfile.txtand deletesfile.txt.gz. - Preservation: To retain original files, you must explicitly supply
-k(--keep) or stream through stdout (-c). - Single-File Scope:
gzipdoes not package multiple files into an archive. Archiving directory hierarchies requires pairing withtar(producing.tar.gz).
| Option | Long Option | Description | Default |
|---|---|---|---|
-d |
--decompress |
Decompress specified .gz files (equivalent to gunzip). |
Compress |
-c |
--stdout |
Write output to stdout; leave original files untouched. | Replace file |
-k |
--keep |
Keep (do not delete) original input files during processing. | Delete originals |
-f |
--force |
Force overwrite of existing destination files or compress symlinks. | Prompt / Abort |
-l |
--list |
List compressed size, uncompressed size, and ratio for archives. | Normal run |
-t |
--test |
Test the cryptographic and checksum integrity of compressed files. | Decompress |
-v |
--verbose |
Display compression percentage reduction and filenames. | Silent |
-r |
--recursive |
Traverse directory trees recursively, compressing every file inside. | Top-level only |
-q |
--quiet |
Suppress all non-fatal warning messages. | Normal |
-n |
--no-name |
Do not save or restore original filename and timestamp. | Save name/mtime |
gzip allows regulating the trade-off between compression speed and file size reduction using numeric flags:
| Flag | Mode | Description |
|---|---|---|
-1 |
--fast |
Fastest compression speed; lower compression ratio. |
-6 |
Default balance of speed and compression efficiency. | |
-9 |
--best |
Maximum compression ratio; slowest execution speed. |
| Operation | Command Pattern | Copyable One-Liner | Notes |
|---|---|---|---|
| Compress keep original | gzip -k [file] |
gzip -kv access.log |
Compresses file without deleting original |
| Decompress keep archive | gzip -dk [file.gz] |
gzip -dk access.log.gz |
Restores original file, keeps .gz archive |
| Fast compression | gzip -1 [file] |
gzip -1k backup.sql |
Minimum compression time, larger output |
| Maximum compression | gzip -9 [file] |
gzip -9k backup.sql |
Best compression ratio, higher CPU usage |
| Test archive integrity | gzip -t [file.gz] |
gzip -tv archive.tar.gz |
Verifies internal CRC32 checksums |
| Inspect metrics | gzip -l [file.gz] |
gzip -l access.log.gz |
Displays uncompressed size and savings ratio |
| Stream stdout | gzip -c [file] > [out.gz] |
gzip -c file.txt > file.txt.gz |
Safe stream output leaving source untouched |
Compress a file with progress reporting while ensuring the original source file is preserved:
gzip -kv access.logOutput:
access.log: 84.2% -- replaced with access.log.gzVerify that both files remain present:
ls -lh access.log*Output:
-rw-r--r-- 1 user user 100M Sep 12 18:00 access.log
-rw-r--r-- 1 user user 16M Sep 12 18:00 access.log.gzDecompress an archive, restoring the original uncompressed file:
gzip -d -k access.log.gz
# Or use the standard symlink
gunzip -k access.log.gzQuery compressed size, uncompressed size, and space savings without decompressing:
gzip -l access.log.gzOutput:
compressed uncompressed ratio uncompressed_name
16580412 104857600 84.2% access.logValidate that a compressed file is not corrupted or truncated before removing external backups:
gzip -tv access.log.gzOutput:
access.log.gz: OKWhen corruption exists, gzip -t immediately flags CRC check or data integrity failures:
$ gzip -tv corrupted.log.gz
corrupted.log.gz: invalid compressed data--crc errorCompress stdout from arbitrary commands directly to disk without creating intermediate uncompressed files:
# Stream database dump directly through maximum compression
mysqldump --all-databases | gzip -9 > db_backup_2026_09_12.sql.gzDecompress directly into a processing pipeline:
gzip -dc db_backup_2026_09_12.sql.gz | mysqlPairing gzip with standard utility wrappers allows inspecting compressed files without manual decompression:
# View compressed text page-by-page
zless access.log.gz
# Search inside compressed logs without unpacking
zgrep "ERROR 500" access.log.gz
# Compare two compressed files
zdiff file1.txt.gz file2.txt.gzCompress all historical log files within a nested directory hierarchy:
gzip -r -k /var/log/archive/The GZIP specification (RFC 1952) permits multiple compressed members to be concatenated together into a single file. gzip -d transparently decompresses all members sequentially:
# Compress two files independently
gzip -c part1.txt > combined.gz
gzip -c part2.txt >> combined.gz
# Decompressing combined.gz unpacks the concatenated text of both parts
gzip -dc combined.gz > full.txtOn modern multi-core systems, standard gzip is CPU-bound to a single core. The drop-in compatible tool pigz (Parallel Implementation of GZip) splits compression work across all available CPU threads while generating fully compliant gzip files:
# Compress using all CPU cores with pigz
pigz -k -9 huge_disk_dump.raw| Exit Code | Meaning |
|---|---|
0 |
Success: compression, decompression, or integrity test completed without errors. |
1 |
Error: invalid file format, CRC failure, or file read/write error. |
2 |
Warning: warning condition encountered (e.g. file not compressed because it would expand). |
| Variable | Description |
|---|---|
GZIP |
Historical environment variable holding default options (deprecated by upstream due to security hazards with filenames containing spaces). |
Warning
In-Place Source Deletion: By default, gzip deletes the uncompressed source file upon successful compression, and deletes the .gz archive upon decompression. Always pass -k (--keep) or stream with -c to preserve input files.
Caution
Denial of Service via Zip Bombs: Highly repetitive byte sequences can compress gigabytes of data into small megabyte archives. Check uncompressed dimensions using gzip -l before unpacking untrusted archives.
gzip is universal across all Linux distributions, BSD systems, and macOS. Decompressed .gz files comply with RFC 1952 and can be unpacked by any standard deflation tool (e.g. 7-Zip, WinRAR, Python's gzip module).
Tip
Always Supply -k (--keep) in Production Scripts: Preserving input files until external backup integrity verification succeeds prevents catastrophic data loss if subsequent tasks fail.
Upstream Rationale: gzip(1) explicitly defines destructive source deletion as standard default behavior. In automated scripts, unlinking source files before verifying backup integrity invites data loss. Always specify -k to retain the source until external verification succeeds.
Important
Always Validate with -t Before Removing Originals: Run gzip -t <file>.gz to calculate and verify internal CRC32 checksums before cleaning up original uncompressed data.
Upstream Rationale: Silent disk write failures or network interruptions during streaming can produce truncated .gz archives. Running gzip -t <file>.gz calculates and verifies the internal CRC32 checksum before original files are removed.
Upstream Rationale: gzip is strictly a file compression filter; it does not record directory structures, permissions, or multi-file hierarchies. Always bundle directory structures with tar (tar -czf archive.tar.gz directory/) rather than attempting to run gzip -r.
gzip(1)— GNU gzip reference manual: https://www.gnu.org/software/gzip/manual/gzip.html- RFC 1952 — GZIP File Format Specification version 4.3: https://datatracker.ietf.org/doc/html/rfc1952
- GNU gzip Source Repository: https://git.savannah.gnu.org/cgit/gzip.git