Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
gzip notes.txt          # creates notes.txt.gz and removes notes.txt
gunzip notes.txt.gz     # restores notes.txt

Try it

Bash Compress, inspect and restore

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.

Bash Levels, stdin and stdout

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

Terminal
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 file replaces file with file.gz. Use -k or -c if you need both. Old systems without -k can use gzip -c file > file.gz.
  • One file at a time. gzip a b c makes three separate .gz files; it never merges them. For a directory use tar -czf dir.tar.gz dir.
  • Concatenated members are valid. cat a.gz b.gz > both.gz decompresses to a followed by b, 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 -n leaves 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 -c into a terminal fails with "compressed data not written to a terminal". Redirect or pipe it.
  • Parallel gzip. The pigz tool produces compatible output on several cores; plain gzip uses only one.
  • Reading without unpacking: zcat, zgrep and zless read .gz files directly.
  • -l reports 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 .gz files.