gzip is the command everyone half-remembers until the moment they actually need to shrink a file or unpack one. This guide compresses ordinary files to .gz, checks the result, restores it with gunzip, and streams compressed data without an intermediate file. Examples match GNU gzip 1.12 from the Ubuntu package gzip 1.12-1ubuntu3.2. Allow about ten minutes for a first pass. You need a shell and read and write access to a test directory; none of the normal commands need elevated privileges.
The four names in this guide are views of the same gzip interface. gzip compresses, gunzip expands, uncompress is a compatibility name, and zcat expands to standard output. Confirm what your shell will run:
$ command -v gzip gunzip uncompress zcat
/usr/bin/gzip
/usr/bin/gunzip
/usr/bin/uncompress
/usr/bin/zcat
$ gzip --version
gzip 1.12
Keep the version in mind when comparing output with another machine. This guide describes the installed GNU implementation, not a generic promise about every program called gzip.
Create or choose a disposable text file for the first test. The ordinary form of gzip replaces the input with a file whose name ends in .gz:
$ printf 'alpha\nbeta\n' > report.txt
$ gzip report.txt
$ ls -l report.txt.gz
-rw-r--r-- 1 you you 42 Sep 24 12:00 report.txt.gz
After successful compression, report.txt is normally gone and report.txt.gz is present. gzip preserves the file mode and modification time where it can, and records the original name and timestamp in the compressed data by default. A short input can end up slightly larger, because the gzip header and checksum still take space.
Checkpoint: If the original must remain available, remove the test output and start again with -k, or use a copy of the source. Do not use rm on an input you still need.
Use --keep when the source is part of your working set, or is the only easy recovery copy:
$ printf 'gamma\n' > keep.txt
$ gzip --keep keep.txt
$ test -f keep.txt && test -f keep.txt.gz && echo 'source and compressed copy exist'
source and compressed copy exist
This adds the compressed copy to the directory but leaves the source untouched. The same option is useful with decompression, when you want both the archive and the restored file.
Use --list for the stored sizes and original name:
$ gzip --list report.txt.gz
compressed uncompressed ratio uncompressed_name
42 11 -18.2% report.txt
The exact byte counts and ratio depend on your input. A negative ratio is possible for a very small file, because the compression metadata is larger than the saved text; it is not evidence that the command failed.
Use --test to check the compressed stream and its CRC without writing the restored file:
$ gzip --test report.txt.gz
$ printf 'exit status: %s\n' "$?"
exit status: 0
A status of zero means the integrity check passed. It does not mean the file contains the content you wanted, so inspect the name, size or content as well.
The simplest restore command is:
$ gunzip --keep report.txt.gz
$ test -f report.txt && echo 'restored report.txt'
restored report.txt
Without --keep, successful decompression normally removes the .gz input and leaves the uncompressed file. gzip also understands several older suffixes, including .z, .Z, .tgz and .taz, but the magic number still has to identify a supported format: a file name alone is not proof its contents are gzip data.
If a destination already exists, gzip asks before overwriting it when it can. Treat that prompt as a stop sign and check which file is the useful one. --force permits replacement and also overrides some safety checks, so use it only after checking the exact input and destination. There is no undo for an overwritten file, so make a backup or write to a new name first.
With no file name, or with -, gzip reads standard input. With --stdout, it writes compressed data to standard output and leaves named input files unchanged:
$ printf 'one\ntwo\n' | gzip > lines.txt.gz
$ zcat lines.txt.gz
one
two
zcat is equivalent to gunzip --stdout, and can also read a gzip stream from standard input:
$ printf 'pipeline data\n' | gzip | zcat
pipeline data
Handy for a one-off inspection, or for feeding a decompressed stream to another command. Nothing is saved unless you redirect it, and shell redirection truncates an existing destination before the command runs, so check that destination first.
gzip compresses files; it is not an archive manager. For a directory tree, use an archiver such as tar with gzip support, for example tar -czf backup.tar.gz DIRECTORY. Read the tar manual before using that command on data you cannot recreate, and verify the archive afterwards. Do not expect gzip alone to preserve a directory structure.
When several inputs go to standard output, gzip creates a sequence of compressed members. A decompressor can emit them in order, but combining unrelated files this way loses the original file boundaries. Use tar or another archive format when you need independent extraction.
If gzip reports corrupt input, leave the damaged file unchanged. The installed manual says data before the failure point may be recoverable through standard output:
$ zcat damaged.txt.gz > recovered.txt
$ printf 'zcat exit status: %s\n' "$?"
zcat exit status: 1
The recovered file may be incomplete, and the non-zero status is expected when the stream is damaged. Compare it with another copy or checksum before trusting it, and do not delete the damaged input until recovery and verification are complete.
gzip --test passes for compressed files you intend to use.gunzip restores a file, or zcat streams it without creating one.