Archives and compression

Command gzip

Compress file using gzip algorithm

Linux · Bash examples · commands run on the selected server

When to use gzip

Use gzip to compress individual streams or files. Retain originals when needed, and distinguish a valid compressed stream from a trustworthy or complete backup of your application data.

Syntax

gzip [OPTIONS] FILE…

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

-k
Save source.
-d
Unpack .gz.
-t
Check the integrity of the stream.

Usage examples

Compress text while preserving the source

gzip -k ./report.txt

A compressed file is produced while retaining the original.

Read unpacked text without writing

gzip -dc ./report.txt.gz

The decompressed contents are sent to standard output without replacing the compressed file.

Check compressed file

gzip -t ./report.txt.gz

Stream integrity is tested without writing an extracted copy.

Notes and limitations

  • gzip compresses the stream and does not itself store a directory tree.
  • Without -k, normal compression replaces the original file with .gz; the -c switch directs the result to standard output.

Common questions and troubleshooting

Can gzip package a whole directory by itself?

It compresses data streams rather than preserving a directory archive structure. Combine an archiver with compression for a directory backup.

Why is the output still large?

Already compressed or encrypted content may not compress much further. Compression ratio does not indicate whether the operation succeeded.

Further help

Options vary by version and distribution. Check the installed documentation with this command: man gzip.