The gzip Command in Linux

The gzip Command in Linux

A single `report.log.gz` and a `logs.tar.gz` can both look like compressed files, but only one is an archive of directory paths. I couldn’t leave that distinction alone, because it decides which command gives you the file you meant to create.

Once the output’s shape is clear, you can match the command to keeping a source file or packaging a directory together. It comes down to what gzip does to a file.

What gzip does to a file

gzip compresses file data into a .gz stream, then removes the source after compression succeeds.

A .gz file stores one compressed stream, not a collection of directory paths.

Use tar first when the recipient needs several files or folders together, then let gzip compress that archive.

TaskCommand shapeResult
Compress one filegzip report.logreport.log.gz replaces report.log
Keep the sourcegzip -k report.logBoth files remain
Compress each file under a directorygzip -r logs/Separate .gz files in the directory tree
Bundle paths and compress themtar -czf logs.tar.gz logs/One archive, source paths remain

GNU gzip uses DEFLATE for the compressed data. The GNU gzip manual describes its file and stream options, while the GNU tar manual covers archive creation.

Choose where the compressed bytes go

Before running gzip, decide whether you need the original file and where the compressed output should go. The default replaces the source only after compression succeeds.

gzip -kv access.log

The -k option keeps access.log, and -v prints the resulting size change. I ran this command on a 44-byte sample, and GNU gzip 1.12 reported an 11.4% reduction while creating access.log.gz.

gzip -kv reports that access.log.gz was created with an 11.4% reduction
gzip -v reports the size change and -k keeps the source file.
gzip -k keeps the uncompressed original alongside the compressed archive
The -k option leaves the input beside its compressed copy.

Use -c when a pipeline or redirection should receive the compressed bytes instead of replacing the named input.

gzip -c access.log > access.log.gz
printf 'streamed line\n' | gzip > stream.gz

With no filename, gzip reads standard input and writes to standard output. Redirection sends the compressed stream to a file.

FormInputSource file
gzip fileNamed fileReplaced after success
gzip -k fileNamed fileKept
gzip -c fileNamed fileKept, compressed bytes go to stdout
gzip with no filenameStandard inputNo named source to replace

Compress, restore and inspect files

Use gzip on a filename to create its .gz copy.

gzip -v access.log
Gzip command compresses a file and replaces the original with a .gz archive
After compression succeeds, the .gz output takes the source file’s place.

To restore the original filename, use -d or its command-name equivalent, gunzip. A successful restore removes the compressed input unless you add -k.

gzip -dv access.log.gz
gzip -dkv access.log.gz
gzip -d restores the original file and removes the compressed archive
Decompression restores the original filename and normally removes the .gz file.

Add -k when the compressed input should remain after the restore.

gzip -dkv access.log.gz

Compress multiple files separately

Pass several filenames when each one should have its own compressed output. gzip reports each result separately with -v.

gzip -v first.log second.log
gzip -v reports separate outputs and shows that a small file can grow after compression
Each input gets a separate .gz output, and the second file grows after compression.

A negative percentage means that compressed output grew. gzip still appends .gz, so compare the reported sizes rather than assuming the file shrank.

Use -l to inspect the stored filename and size fields without extracting the file.

Use -t when you need gzip to read the full compressed stream and report whether it is intact.

gzip -lv access.log.gz
gzip -t access.log.gz
zcat access.log.gz | grep -n ERROR
gzip -t accepts an intact access.log.gz archive and prints Archive is valid
An intact compressed stream passes the gzip integrity test.

A truncated stream fails instead. The error tells you gzip reached the end of the file before it could finish reading the compressed data.

gzip -t broken.log.gz
gzip -t reports an unexpected end of file for a truncated gzip archive
A truncated archive fails the integrity check with an unexpected end of file.

zcat writes decompressed data to standard output and leaves the archive in place. Pipe it to grep to inspect matching lines, or use the cat and more commands to read the resulting plain text.

Recursive compression does not make one archive

The -r option visits a directory tree and compresses eligible files separately.

It does not bundle their paths, and the source files are replaced as each compression succeeds.

gzip -r recursively compresses every file in a directory
Recursive gzip creates separate compressed files under the directory tree.

In my GNU gzip 1.12 test, recursive traversal left a symlink unchanged and reported “Too many levels of symbolic links.” The command still compressed the regular file it reached.

CommandOutput
gzip -r logs/Separate compressed files under logs/
tar -czf logs.tar.gz logs/One compressed archive containing the paths

GNU tar leaves the source directory in place. See the Linux tar command guide or learn to count files in a directory before processing it.

Check the destination before you overwrite it

gzip refuses to overwrite an existing output unless you force it. Decompression also stops when the restored filename already exists.

  • Use -k to preserve the input during compression or decompression.
  • Move an existing destination aside when you need the restored file.
  • Use -f only when replacing the destination is intentional.

A tiny file can grow after compression because the gzip header and trailer add bytes. Check the resulting size when that distinction matters.

OptionBehaviorUseful when
-1Faster compression, less compression effortSpeed matters more than a smaller output
-6Default levelOrdinary file compression
-9Slower compression, more effort to reduce sizeYou measure a useful reduction for your files
Level 9 compression produces the smallest file at the cost of speed
This terminal capture shows gzip -9 output, not a comparison between levels.

GNU gzip describes -1 as the fastest level and -9 as the slowest. This screenshot shows only level 9, so it does not establish which level is faster for your own files.

For a directory sorted by size, use the Linux directory-size listing command. If the recipient needs a ZIP archive rather than .gz streams, use the zip and unzip commands.

Verify the archive before sharing it

Run gzip -t on the completed file before you send it or remove another copy.

gzip -t logs.tar.gz

A successful exit means gzip could read the stream to its end. It does not confirm that an archive contains the paths you intended, so list tar members separately with tar -tzf.

FAQ

Does gzip delete the original file?

By default, gzip replaces a regular input with its .gz output after compression succeeds. Use gzip -k to keep both files, or gzip -c with redirection to write compressed bytes elsewhere.

How do I compress a directory into one file?

Use tar -czf archive.tar.gz directory/. gzip -r compresses eligible files separately and does not bundle directory paths.

How can I test a gzip file without extracting it?

Run gzip -t filename.gz. It reads the compressed stream and returns a failure status if the data is incomplete or invalid.

Can gzip make a file larger?

Yes. A very small input can grow because gzip adds header and trailer data. Check the resulting size for your own file.