#

Commands

Subscribe
Commands October 29, 2026·7 min read

Gzip and Gunzip in Linux: Compress, Extract, and Keep Files

Use gzip to compress a file and gunzip to decompress it, adding -k to keep the original. Use tar to pack a directory into one compressed archive. Check the file format and destination before changing important data, then verify the result.

What you needCommand or toolExpected result
Compress one filegzip file.logOnly file.log.gz
Compress and keep the originalgzip -k file.logBoth files remain
Decompress a .gz filegunzip file.log.gzOnly file.log
Decompress and keep the archivegzip -dk file.log.gzBoth files remain
Package an entire directorytar with gzip compressionOne .tar.gz archive
Produce a ZIP fileZIP toolA .zip archive

Which should you use: gzip, gunzip, tar.gz, or ZIP?

gzip archive formats

GNU gzip compresses file contents or a data stream. A plain .gz file holds one compressed input. tar collects multiple files and directories into an archive. Combine the two and you get a .tar.gz or .tgz file.

Take three filenames. server.log.gz is a compressed log you can decompress or inspect with gzip tools. backup.tar.gz is a compressed tar archive of file entries, and GNU tar extracts it. submission.zip is a ZIP archive.

If a downloaded file named download.gz refuses to decompress, check its real format with the file utility:

file download.gz

This helps when a server sends a wrongly named download or someone renamed a file without changing its format.

Compress a file and keep the original

gzip file compression

For a file named sample.log, run:

gzip sample.log

gzip deletes sample.log once it has written sample.log.gz.

To keep the original, add -k. gzip then leaves sample.log next to sample.log.gz:

gzip -k sample.log

Practice on a disposable file

mkdir gzip-practice && cd gzip-practice && printf 'INFO started\nERROR example\n' > sample.log

Compress with -k and list the directory:

gzip -k sample.log
ls -1

The listing shows both files:

sample.log
sample.log.gz
gzip file results

Decompress a .gz file without discarding the archive

gunzip file extraction

gunzip reverses gzip compression. gzip -d does the same, and -k keeps the compressed input. For sample.log.gz, use one of these:

gunzip sample.log.gz
gzip -d sample.log.gz
gzip -dk sample.log.gz
Starting stateCommandFiles after successful decompression
Only sample.log.gz existsgunzip sample.log.gzsample.log
Only sample.log.gz existsgzip -d sample.log.gzsample.log
Only sample.log.gz existsgzip -dk sample.log.gzsample.log and sample.log.gz

The practice directory above already contains sample.log, so try gzip -dk in a separate folder that holds only the .gz file.

Compress multiple files or a directory: gzip -r vs tar

recursive gzip compression

A student working in Lubuntu described the problem in r/linuxquestions: “I then opened the terminal and used gzip -r to compress the directory but instead it compressed every file inside the directory, no the directory itself.”

gzip -r does exactly that. It goes through a directory tree and compresses each eligible file on its own. Take this directory:

reports/
  jan.log
  feb.log

Run:

gzip -r reports/

The result is two separately compressed files:

reports/
  jan.log.gz
  feb.log.gz

Create one compressed archive with tar

Starting from a separate original copy of reports/ with the two uncompressed logs, use GNU tar:

tar -czf reports.tar.gz reports/

-c creates the archive, -z applies gzip compression, and -f sets the output filename.

tar directory archive

tar packs a directory into one transferable file and leaves the original directory in place. A submission that requires ZIP needs a real ZIP archive, because .tar.gz is a different format.

View and search compressed text without extracting it

compressed log inspection

zgrep 'ERROR' app.log.gz prints matching lines from a compressed log, and no restored copy lands on disk. zless app.log.gz opens the contents for browsing. zcat app.log.gz sends the full decompressed text to standard output. Don't send a very large or binary file through zcat to the terminal, because the output becomes unwieldy or unreadable.

Choose a compression level or write to standard output

gzip compression options

gzip has compression levels that trade effort against output size.

  • -1: Faster compression.
  • -6: The documented default.
  • -9: More effort for a smaller result.

Data that is already compressed gains little.

To compare levels, run two of them on a representative, noncritical file and write to separate output names:

gzip -1 -c sample.log > speed.gz
gzip -9 -c sample.log > size.gz

sample.log stays in place. Compare the two output sizes.

Send compressed data to a new output filename

-c writes the output to standard output (stdout), and shell redirection puts it in a file:

gzip -c sample.log > stream-copy.gz

This creates stream-copy.gz and keeps sample.log.

Use a new output filename: The shell's > operator can truncate an existing file before gzip runs, even if your gzip options preserve the input.

gzip stdout redirection

Decompress to stdout like this:

gzip -dc sample.log.gz > restored.log

-d selects decompression, -c sends the result to stdout, and the shell writes it to restored.log.

Test a .gz file and check the restored data

gzip integrity verification

gzip -t tests the internal consistency of a compressed stream and writes no output file.

gzip -t sample.log.gz

A damaged stream triggers an error and a nonzero exit status.

What the gzip integrity check means

The gzip format stores a CRC32 value calculated from the uncompressed data and an ISIZE field with the uncompressed length modulo 232. RFC 1952 defines both.

CRC32 detects errors. A valid gzip stream doesn't prove who produced it.

Compare recovered bytes with a trusted original

If you still have the original sample.log from the disposable exercise, compare it with the decompressed stream:

gzip -dc sample.log.gz | cmp - sample.log

cmp reads its first input from standard input when you pass -. A matching result means the bytes it received are identical to those in sample.log. A pipeline reports only the final command's status, so it can hide a gzip error unless pipefail is enabled. Run gzip -t sample.log.gz first, and count the data as verified only when both the test and the comparison succeed.

gzip data comparison

gzip -l lists information about compressed files. Its uncompressed size is unreliable for very large inputs and concatenated gzip members, so don't treat it as a byte-accurate measurement.

Troubleshoot gzip errors without making the problem worse

gzip error diagnosis
SymptomPossible causeFirst safe diagnostic
not in gzip formatWrong file type, misleading extension, or a stream already decompressedRun file on the input, and in a pipeline check which earlier step already decompressed it
Destination already existsThe output name matches an existing fileKeep the existing file and check what it contains
Corrupt or truncated gzip dataThe compressed stream failed its integrity checkRun gzip -t on the file
Permission or storage errorThe output location isn't writable or is out of spaceCheck write permission and free space on the output folder
Failure after a decompression pipelineAn earlier step already removed the gzip layerFind which program in the pipeline did the decompression
Empty redirected outputThe command didn't send the intended data to stdoutCheck the -c and -d options

When a pipeline decompresses gzip twice

“How is the gzipped archive not in gzip format?” a reader asked in r/linuxquestions while troubleshooting a decompression pipeline.

In that pipeline, gunzip -c had already removed the gzip layer before tar tried to decompress the stream again. Once the first step produces a tar stream, a second gzip step fails on it.

If you have a real gzip-compressed tar archive as a regular file, GNU tar handles the compression itself. Extract into a new directory so existing reports/ files stay untouched:

mkdir restored-test && tar -xf reports.tar.gz -C restored-test

tar detects the compression, decompresses it, and puts the reports/ tree inside restored-test/.

When the restored filename already exists

Say sample.log.gz should decompress to sample.log, but a file with that name already exists. Keep the existing file until you know whether it holds data you still need.

In an isolated gzip 1.13 test, gzip -dk sample.log.gz encountered an existing sample.log, returned exit status 2, and left that destination unchanged.

gunzip output conflict

Don't use -f to force past an unexplained collision, because it changes overwrite behavior. A truncated download needs another copy. Don't change permissions or delete unrelated files at random to get past a permission or space error.

NoScript Team
© 2026 No Script