Linux Command Reference ยท Archives and Compression
Linux gzip and gunzip commands
Reference for the Linux gzip and gunzip commands: compress and decompress files, keep originals with -k, choose levels 1-9, work with pipes, test with -t and list ratios.
gzip compresses files with the DEFLATE algorithm and is the most widely supported compressor on Unix systems and the web. It replaces each file name by name.gz, gunzip (the same program with -d) reverses that. It compresses a single stream, so to pack a whole directory you combine it with tar.
gzip is fast, uses little memory and its output is understood everywhere, which is why log rotation, package downloads and HTTP all default to it. Its compression ratio is lower than that of bzip2 or xz.
gzip notes.txt # creates notes.txt.gz and removes notes.txt gunzip notes.txt.gz # restores notes.txt
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
gzip [OPTION]... [FILE]... gunzip [OPTION]... [FILE.gz]...
| Option | Description |
|---|---|
-d, --decompress |
Decompress (same as gunzip). |
-k, --keep |
Keep the input files. Available in gzip 1.6 and later. |
-c, --stdout |
Write to standard output, leave the files alone. |
-1 ... -9 |
Compression level: 1 fastest (--fast), 9 smallest (--best). Default is 6. |
-f, --force |
Overwrite existing output, compress links, even refuse-to-write-terminal cases. |
-r, --recursive |
Descend into directories (compresses each file separately). |
-t, --test |
Check integrity without writing anything. |
-l, --list |
Show compressed and uncompressed sizes and ratio. |
-v, --verbose |
Print name and percentage saved. |
-S SUF, --suffix SUF |
Use SUF instead of .gz. |
-n, --no-name |
Do not store the original file name and timestamp (reproducible output). |
With no file, or -, gzip reads standard input.
Returns: 0 on success, 1 on error, 2 on a warning, such as declining to overwrite an existing .gz file.
Usage notes
- The original disappears by default.
gzip filereplacesfilewithfile.gz. Use-kor-cif you need both. Old systems without-kcan usegzip -c file > file.gz. - One file at a time.
gzip a b cmakes three separate.gzfiles; it never merges them. For a directory usetar -czf dir.tar.gz dir. - Concatenated members are valid.
cat a.gz b.gz > both.gzdecompresses toafollowed byb, a handy trick for appending to compressed logs. - Level trade-off. Level 1 is roughly 2-3 times faster than 9 on typical data, but 9 rarely beats 6 by more than a few percent. Already compressed data (JPEG, MP4, ZIP) hardly shrinks.
- Reproducible output:
gzip -nleaves the name and time out, so identical content gives identical bytes; this matters for caching and package builds. - Refuses to write a terminal.
gzip file -cinto a terminal fails with "compressed data not written to a terminal". Redirect or pipe it. - Parallel gzip. The
pigztool produces compatible output on several cores; plain gzip uses only one. - Reading without unpacking:
zcat,zgrepandzlessread.gzfiles directly. -lreports sizes modulo 4 GiB for files over that size in older versions; do not trust it for huge files.- HTTP. Web servers can send content compressed with gzip when the client accepts it; the format is the same as
.gzfiles.
Related
bzip2,xz,zstd,tar,zcatandzgrep- Lessons: Archives and compression, Pipes