Restore gzip-Compressed Data
Restore gzip-Compressed Data
The teacher has a gzip-compressed mark sheet and wants to read the original text again. gunzip reverses that gzip encoding. It does not recover data that was never in the stream, and it does not unpack a named directory tree just because the file is compressed.
The choice we will make is whether restoration replaces a saved file or writes bytes to standard output. Output mode is useful when you want to inspect the restored text while retaining the compressed input.
What you’ll learn
- Restore a gzip-compressed report with gunzip.
- Read decompressed output while retaining the compressed input.
Decompression can write to output or replace a file
Compressed input
The gzip stream encodes the report bytes.
marks.txt.gzOutput mode
gunzip -c writes decompressed bytes to standard output.
plain textRetained input
This mode leaves the gzip file in place.
marks.txt.gz remains
Read restored marks without deleting the gzip file
Prepare the example
Begin in a new directory with gzip tools available. The first two lines create a mark sheet and compress it into a separate file. That prepares the exact input used by the decompression command, so you can follow the complete round trip in this section.
Bash · Linux terminal
printf 'Diya: 72\n' > marks.txt
gzip -c marks.txt > marks.txt.gz
gunzip -c marks.txt.gzExpected result
Diya: 72gzip -c creates the compressed stream without replacing the original. gunzip -c marks.txt.gz then sends the decompressed bytes to standard output, so the terminal displays the mark line. It does not delete the .gz file in this output mode.
If you want a second saved restored file, you can redirect that output to a different pathname. Avoid choosing the same path as the compressed input: the shell can truncate the destination before decompression reads it. Also distinguish a content-reading operation from gunzip -t, which checks stream integrity without printing the restored report.
Restore a stream or send its bytes to output
gunzip reverses gzip compression. A typical filename.gz becomes filename after decompression.
Step 1: Check if gunzip is installed
Most Linux distributions come with gunzip pre-installed. But just to be sure, you can run:
gunzip --version
gunzip (gzip) 1.10
Copyright (C) 2007 Free Software Foundation, Inc.
If it shows a version number like above, you’re good to go!
Step 2: Navigate to the folder with the .gz file
Use the cd command to move to the directory where your compressed file is located:
cd Downloads
Step 3: Use gunzip to extract the file
Now let’s say you have a file named data.txt.gz. To extract it, run:
gunzip data.txt.gz
(no output means success!)
After extraction, you’ll see a new file called data.txt, and the original .gz file will be removed by default.
Want to keep the original .gz file?
If you'd like to keep the original compressed file, use the -c option and redirect the output:
gunzip -c data.txt.gz > data.txt
This will decompress the content but preserve the original .gz file.
Step 4: Verify the extracted file
You can now open or view the file using commands like:
cat data.txt
This is sample data inside the file.
Extract multiple .gz files at once
If you have many files ending in .gz, you can extract them all in one command:
gunzip *.gz
Restore gzip-compressed files with gunzip
Now you know how to use gunzip to extract compressed files in Linux confidently. Whether it's a single file or a whole folder full of .gz archives, you're ready to handle them with confidence.
Try it, then check the worked answer
Your task: Read restored bytes without deleting the gzip file.
Begin in a new directory with gzip tools available. The first two lines create a mark sheet and compress it into a separate file. That prepares the exact input used by the decompression command, so you can follow the complete round trip in this section.
Bash · complete example for this question
printf 'Diya: 72\n' > marks.txt
gzip -c marks.txt > marks.txt.gz
gunzip -c marks.txt.gzExpected result
Diya: 72Worked answer
gunzip -c marks.txt.gz
Choose gunzip -c to read restored bytes while keeping the gzip file. The ordinary replacement form commonly removes the compressed input after successful restoration. The integrity-test option answers a different question and does not display the report.
Check your decision
Choose output mode when both forms should survive
Decompression restores the bytes represented by the chosen format. Decide whether you need output, a saved restored file or replacement, and protect input paths from redirection mistakes. If the gzip stream contains a tar archive, tar extraction is a further step rather than the same operation.
Reaching the recap and completing the quizzes are separate milestones.