Restoring a gzip-compressed log, dump, or downloaded file is easy to rush because gunzip removes the .gz input after a successful decompression. The gunzip command in Linux is safer when you know when to keep the compressed copy, when to stream the decompressed text, and when a .tar.gz file needs tar instead of plain gunzip.
Choose the Right gunzip Pattern
gunzip decompresses files created with gzip compression. It is equivalent to gzip -d, so most GNU gzip decompression options also work through the gunzip command name. By default, gunzip file.log.gz restores file.log and removes file.log.gz after the operation succeeds.
The basic syntax follows this pattern:
gunzip [OPTION]... [FILE]...
[OPTION]...: Decompression, output, testing, metadata, or overwrite flags such as-k,-c,-t,-l,-N,-S, or-f.[FILE]...: One or more gzip-compressed files. When no file is named,gunzipreads compressed data from standard input and writes decompressed data to standard output.
Use these common patterns as a quick reference before changing files:
| Task | Command Pattern | What It Does |
|---|---|---|
| Restore one file | gunzip file.log.gz | Creates file.log and removes file.log.gz after success. |
| Keep the compressed copy | gunzip -k file.log.gz | Creates file.log while leaving file.log.gz in place. |
| Write output to another path | gunzip -c file.log.gz > restored.log | Sends decompressed data to standard output so redirection controls the destination. |
| Read from standard input | gunzip < file.log.gz > restored.log | Accepts compressed data from a pipe or redirection and writes decompressed text elsewhere. |
| Search a compressed log | zgrep -n 'ERROR' file.log.gz | Searches inside gzip data without creating a restored file first. |
| Show the end of a compressed log | zcat file.log.gz | tail -n 20 | Streams decompressed text to another command for inspection. |
| Test a gzip file | gunzip -t file.log.gz | Checks whether the gzip stream can be read without writing an output file. |
| List gzip metadata | gunzip -l file.log.gz | Shows compressed size, uncompressed size, ratio, and inferred output name. |
| Decompress a directory tree | gunzip -r -k logs | Walks through a directory and decompresses gzip files it finds. |
| Decode a gzip file with no suffix | gunzip -c download > restored.log | Uses standard output when gzip data lacks the normal .gz suffix. |
| Restore a saved original name | gunzip -N renamed.gz | Uses the original filename stored in the gzip header when that metadata exists. |
| Use a custom suffix | gunzip -S .gzip file.gzip | Treats a nonstandard suffix as the gzip suffix to remove. |
A plain .gz file stores one compressed stream, not a folder of files. If the file is .tar.gz or .tgz, gunzip only removes the gzip layer and leaves a .tar archive behind. Use tar when the goal is to extract the files inside that archive.
Verify gunzip Availability
Normal Linux desktop and server installations usually include gunzip with the gzip package. Check the active commands before troubleshooting minimal images or stripped-down containers:
command -v gunzip gzip zcat zgrep
Expected output shows each helper on your PATH:
/usr/bin/gunzip /usr/bin/gzip /usr/bin/zcat /usr/bin/zgrep
Confirm the implementation when GNU-specific options matter:
gzip --version | head -n 1
GNU gzip prints a short version line. The exact number depends on the distribution release:
gzip 1.14
The GNU gzip manual documents the complete option set when you need less common behavior such as custom suffixes, original-name restoration, or header metadata controls.
If gunzip is missing, install the distro package named gzip. On Ubuntu and Debian-based systems, use APT:
sudo apt install gzip
On Fedora, Rocky Linux, and other DNF-based systems, use DNF:
sudo dnf install gzip
After installation, rerun command -v gunzip. On other distributions, search for the gzip package before installing on minimal systems, because smaller images can use BusyBox applets with a reduced option set.
Create a Disposable gunzip Practice Directory
A small practice directory keeps decompression, overwrite, and troubleshooting examples away from real logs or backups. Create the files in your home directory, then work from that directory:
mkdir -p ~/gunzip-demo/{batch,tree/sub,project,restored}
printf 'INFO start\nERROR disk full\nINFO done\n' > ~/gunzip-demo/app.log
printf 'first\n' > ~/gunzip-demo/batch/first.log
printf 'second\n' > ~/gunzip-demo/batch/second.log
printf 'tree one\n' > ~/gunzip-demo/tree/one.log
printf 'tree two\n' > ~/gunzip-demo/tree/sub/two.log
printf 'config=true\n' > ~/gunzip-demo/project/config.txt
printf 'readme\n' > ~/gunzip-demo/project/readme.txt
gzip -k ~/gunzip-demo/app.log ~/gunzip-demo/batch/first.log ~/gunzip-demo/batch/second.log ~/gunzip-demo/tree/one.log ~/gunzip-demo/tree/sub/two.log
rm ~/gunzip-demo/tree/one.log ~/gunzip-demo/tree/sub/two.log
cd ~/gunzip-demo
The practice tree contains plain files, compressed copies, and a tree directory that contains only compressed inputs for the recursive example. The mkdir command guide covers nested directory creation and -p behavior in more detail.
Decompress gzip Files with gunzip
The main gunzip decision is whether the compressed input should disappear after the restored file is written. Use the default behavior only when the .gz file is disposable or backed up somewhere else.
Restore a File and Remove the Compressed Input
Work on a copy first so the replacement behavior is visible:
cp app.log.gz default.log.gz
gunzip default.log.gz
ls -1 default.log*
Only the restored file remains after a successful default decompression:
default.log
This behavior is useful for local cleanup, but it is a poor default for downloads, backups, and logs that you may need to compare again.
Keep the Compressed Copy with -k
Add -k when you want the restored file and compressed source to stay side by side:
cp app.log.gz keep.log.gz
gunzip -k keep.log.gz
ls -1 keep.log*
The restored file and the original compressed copy are both present:
keep.log keep.log.gz
Use -k for files from another system, downloaded artifacts, or any workflow where deleting the compressed copy would make retrying harder.
Write Decompressed Output to Another Path
The -c option writes decompressed data to standard output and keeps the input unchanged. Redirect that stream when a script needs a specific output path:
gunzip -c app.log.gz > restored/app.log
cat restored/app.log
The restored text lands in the file you selected:
INFO start ERROR disk full INFO done
zcat app.log.gz is a shorter way to print decompressed text to the terminal. Use gunzip -c when the decompression step should stay visually tied to gunzip in a script or explanation.
Decompress gzip Data from Standard Input
When compressed data arrives from another command or redirection, omit the file operand and let gunzip read from standard input:
gunzip < app.log.gz > restored/stdin.log
cat restored/stdin.log
The restored file contains the same decompressed text as the named-file example:
INFO start ERROR disk full INFO done
Decompress Multiple gzip Files
gunzip accepts more than one file operand. Keep the compressed inputs when you are processing a batch for the first time:
cp batch/first.log.gz multi-one.log.gz
cp batch/second.log.gz multi-two.log.gz
gunzip -k multi-one.log.gz multi-two.log.gz
ls -1 multi-*
Each input now has a restored copy:
multi-one.log multi-one.log.gz multi-two.log multi-two.log.gz
For broad shell patterns, preview the files before gunzip receives them:
printf '%s\n' multi-*.gz
The preview should show only the compressed files you intend to process:
multi-one.log.gz multi-two.log.gz
Shell globs expand before gunzip runs. If a pattern is too broad, it can include old backups, partial downloads, or files that should stay compressed.
Recursively Decompress gzip Files in a Directory
The -r option walks through directories and decompresses gzip files it finds. The practice tree directory contains only compressed files, which avoids overwrite prompts during the example:
gunzip -r -k tree
find tree -type f | sort
The recursive run restores each file while preserving the compressed inputs:
tree/one.log tree/one.log.gz tree/sub/two.log tree/sub/two.log.gz
If an output file already exists, gunzip refuses to overwrite it unless you use -f. That refusal is usually useful because it prevents a restored file from replacing a local copy you may still need.
Read and Search gzip Files Without Extracting
Rotated logs and downloaded reports are often worth checking before restoration. Standard-output workflows let you inspect, search, and pipe gzip data while leaving the compressed file untouched.
Print Decompressed Text to the Terminal
Use gunzip -c without redirection when you want a quick look at a small compressed text file:
gunzip -c app.log.gz
The command writes decompressed text to standard output and keeps app.log.gz unchanged:
INFO start ERROR disk full INFO done
Search Compressed Logs with zgrep
zgrep searches gzip-compressed files directly, which is cleaner than restoring a log only to run grep command examples against the temporary copy:
zgrep -n 'ERROR' app.log.gz
The -n flag prints the matching line number inside the decompressed stream:
2:ERROR disk full
Show Recent Lines from a Compressed Log
zcat is equivalent to gunzip -c, so it works well in pipelines. Combine it with the tail command when the useful lines are near the end of a compressed log:
zcat app.log.gz | tail -n 2
The pipeline prints the last two decompressed lines without creating a restored log file:
ERROR disk full INFO done
Decompress Concatenated gzip Members
Concatenated gzip streams are valid gzip data. This is useful when logs or exported chunks were appended together, but it does not restore separate filenames or directory structure:
cat batch/first.log.gz batch/second.log.gz > combined.gz
gunzip -c combined.gz
The decompressed output is the two original streams one after the other:
first second
Inspect and Test gzip Files
Testing and metadata checks are read-only, so they should come before deletion, replacement, or bulk decompression when a file comes from an unfamiliar source.
Test gzip Integrity Before Decompression
gunzip -t checks whether the gzip stream can be read. Add a success message when you want visible output from a passing test:
gunzip -t app.log.gz && echo "app.log.gz: OK"
A clean gzip file produces the success message:
app.log.gz: OK
List gzip Metadata Without Restoring Files
The -l option prints compressed size, uncompressed size, ratio, and the inferred uncompressed name:
gunzip -l app.log.gz
Example output for the practice file:
compressed uncompressed ratio uncompressed_name
63 37 0.0% app.log
Use this listing for a quick metadata check, not as a compression benchmark. Very small files can show unhelpful ratios because the gzip header is a large part of the final file.
Restore the Original Saved File Name
Some gzip streams store the original filename in their header. The -N option uses that saved name during decompression, which helps when a file was renamed during transfer:
printf 'name demo\n' > original-name.log
gzip -c original-name.log > renamed-transfer.gz
rm original-name.log
gunzip -N renamed-transfer.gz
ls -1 original-name.log
The restored file uses the original name stored by gzip:
original-name.log
This only works when the gzip header contains the original filename. Streams created from standard input or tools that suppress header names may not have a useful name to restore.
Lowercase -n has the opposite meaning during decompression: it ignores the saved original name and timestamp. That is also the default decompression behavior, so only add -N when restoring the stored name is the result you want.
Use a Custom gzip Suffix
Use -S when you inherit files that use a nonstandard gzip suffix. Keep .gz for new files unless another workflow already requires a different suffix:
printf 'suffix demo\n' > suffix.log
gzip -S .gzip suffix.log
gunzip -S .gzip -k suffix.log.gzip
ls -1 suffix.log*
The custom suffix is treated like a gzip suffix, so the restored file and compressed copy can coexist:
suffix.log suffix.log.gzip
Decompress gzip Data Without a .gz Suffix
Normal gunzip file checks the suffix before it replaces a file in place. If a download is gzip data but lacks a .gz suffix, use -c and choose the output path yourself:
cp app.log.gz app-nosuffix
gunzip app-nosuffix
Without standard output mode, GNU gunzip refuses the suffixless file:
gzip: app-nosuffix: unknown suffix -- ignored
Decode the same gzip data through standard output and redirect it to the restored filename:
gunzip -c app-nosuffix > restored/no-suffix.log
cat restored/no-suffix.log
The file content is restored even though the input name did not end in .gz:
INFO start ERROR disk full INFO done
Handle tar.gz, ZIP, and Unknown Formats
gunzip handles the gzip compression layer only. That is exactly right for single-file .gz data, but it is not the full extraction workflow for every archive extension that contains the letters gz.
Decompress a tar.gz File to a tar Archive
A .tar.gz file is a tar archive compressed with gzip. Plain gunzip removes the compression layer and leaves a .tar archive:
tar -czf project.tar.gz project
gunzip -k project.tar.gz
file project.tar
tar -tf project.tar
The result is a readable tar archive, not an extracted project directory:
project.tar: POSIX tar archive (GNU) project/ project/config.txt project/readme.txt
When the reader task is opening or restoring .gz, .tgz, or .tar.gz files, use the GZ and TGZ extraction guide for format-specific extraction patterns. GNU gunzip can decode a narrow single-member ZIP stream that uses deflation, but normal ZIP archives can contain multiple files and directory metadata. Use unzip commands for ZIP archives instead of treating gunzip as a ZIP extractor.
Identify the Real File Type First
Extensions can be wrong, especially after browser downloads, failed redirects, or support uploads. Use file before forcing a decompression path:
file app.log.gz project.tar.gz
A gzip-compressed single file and a gzip-compressed tar archive both report gzip data, so the filename and the next tool choice still matter. If file reports HTML, plain text, ZIP data, or another compression format, switch to the matching tool instead of adding -f to gunzip.
Troubleshoot Common gunzip Errors
Most gunzip failures come from an existing output file, an unknown suffix, the wrong file format, a truncated transfer, permissions, free space, or confusing .tar.gz with a plain .gz file. Match the exact message first, then choose the smallest fix.
Fix Existing Output File Errors
gunzip refuses to overwrite an existing restored file by default. Reproduce the condition in the practice directory by creating the target before decompression:
cp app.log.gz conflict.log.gz
cp app.log conflict.log
gunzip conflict.log.gz
The error names the output path that already exists:
gzip: conflict.log already exists; not overwritten
Compare, move, or back up the existing file before replacing it. If the compressed file should deliberately overwrite the current output, use -f with -k only when the compressed source should also remain:
gunzip -kf conflict.log.gz
cat conflict.log
The restored content replaces the existing output file:
INFO start ERROR disk full INFO done
-f is an overwrite decision, not a repair tool for invalid gzip data. It does not make an HTML page, ZIP archive, or truncated download decompress correctly.
Fix Unknown Suffix Errors
A gzip stream without a recognized suffix produces this message when you ask gunzip to replace the file in place:
gunzip app-nosuffix
gzip: app-nosuffix: unknown suffix -- ignored
Confirm that the file is still gzip data before choosing a workaround:
file app-nosuffix
If file reports gzip-compressed data, either rename the file with a .gz suffix or decode it through standard output:
gunzip -c app-nosuffix > restored/no-suffix.log
cat restored/no-suffix.log
A successful retest prints the restored text:
INFO start ERROR disk full INFO done
Fix “not in gzip format” Errors
A wrong extension, failed download, HTML error page, or plain text file can produce this error. GNU gzip may print a blank line before the message; the useful error line is:
gunzip -t wrong.gz
gzip: wrong.gz: not in gzip format
Check the real file type before retrying decompression:
file wrong.gz
An HTML result means the file is not a gzip stream:
wrong.gz: HTML document, ASCII text
Download the correct file again, switch to the right tool for the reported format, or remove the misleading suffix from a file that was never compressed.
Fix Truncated gzip Files
A partial transfer or interrupted copy can fail integrity testing with this message. GNU gzip may print a blank line before the message; the useful error line is:
gunzip -t broken.gz
gzip: broken.gz: unexpected end of file
Check free space and compare the checksum against the publisher’s expected value before trying another recovery path:
df -h .
sha256sum broken.gz
If the checksum or size does not match the source, download or copy the file again before attempting recovery. Use du disk usage analysis examples when a full filesystem needs cleanup before another transfer attempt.
Fix Permission or Free-Space Failures
Permission and space problems usually appear when the current directory is not writable, the output file is owned by another user, or the destination filesystem is full. Check the working directory and available space before retrying:
pwd
ls -ld .
df -h .
If the current directory is not the right destination, write through standard output to a directory you can modify:
mkdir -p restored
gunzip -c app.log.gz > restored/app.log
test -s restored/app.log && echo "restored"
The retest confirms that a non-empty restored file exists:
restored
Fix tar.gz Files That Become tar Files
If gunzip project.tar.gz leaves project.tar, the command worked on the gzip layer only. List or extract the tar archive next:
tar -tf project.tar
mkdir -p project-restore
tar -xf project.tar -C project-restore
For a one-step extraction from the original compressed archive, use tar -xzf project.tar.gz -C project-restore after creating the destination directory.
Clean Up the gunzip Demo Directory
Remove only the practice directory created for these examples when you no longer need the demo files:
cd ~
rm -rf ~/gunzip-demo
Conclusion
gunzip is safer once decompression is a deliberate choice: keep source files with -k, stream output with -c, search logs with zgrep, and test gzip data before restoring it. When the file is an archive rather than one gzip-compressed stream, switch to the matching tar or unzip workflow instead of forcing gunzip.


Formatting tips for your comment
You can use basic HTML to format your comment. Useful tags currently allowed in published comments:
<code>command</code>command<strong>bold</strong><em>italic</em><blockquote>quote</blockquote>