Compress One File with gzip

Compress One File with gzip

A school's text report may contain repeated patterns that can be represented more compactly. gzip encodes a file into a compressed stream, and a matching decompressor can reconstruct its original bytes. Compression does not discard selected student records; it changes how the complete data is represented.

We will keep the plain report and create a separate compressed version. That is worth choosing explicitly: the usual file-replacement form of gzip behaves differently from its standard-output mode. Also, a tiny or already-compressed file is not guaranteed to become smaller.

What you’ll learn

  • Compress a report and read its compressed contents.
  • Preserve the input deliberately with standard-output mode.

Compressed bytes need a matching decompressor

  1. Plain text

    The original mark report is readable text.

    marks.txt
  2. Compress

    gzip -c writes compressed bytes to standard output, saved with redirection.

    marks.txt.gz
  3. Read again

    gzip -dc writes the restored text without replacing the compressed file.

    Diya: 72
Decide whether you want replacement or separate output before compressing.

Compress the marks while preserving the source

Prepare the example

Start in a fresh practice directory with gzip available. The first line creates the input report. We will use -c and redirection so the source remains in place, then decompress to terminal output rather than replace either saved file.

Bash · Linux terminal

printf 'Diya: 72\n' > marks.txt
gzip -c marks.txt > marks.txt.gz
gzip -dc marks.txt.gz

Expected result

Diya: 72

gzip -c marks.txt writes compressed bytes to standard output. The shell sends them into marks.txt.gz; those bytes are not ordinary text that you should expect to read directly in the terminal. Because output mode is used, the input report remains.

Next, gzip -dc marks.txt.gz requests decompression with -d and standard output with -c. You should see the original mark line again. That round trip helps verify the intended format and contents. A suffix such as .gz helps identify the expected tool, but it is the compressed data itself that makes this a gzip stream.

Replacement, size checks and restoring the input

gzip compresses individual files and normally replaces the original file with a compressed file ending in .gz. It does not by itself combine several files into one archive.

Goal of this Lesson

By the end of this short tutorial, you'll be able to compress a file using gzip, and also know how to decompress it.

Step 1: Create a Sample File (Optional)

If you want to test gzip on a file, you can quickly create one like this:

echo "This is a sample file for testing gzip compression." > sample.txt

Let’s check the file:

cat sample.txt
This is a sample file for testing gzip compression.

Step 2: Compress the File Using gzip

Now let’s compress it:

gzip sample.txt
ls
sample.txt.gz

Step 3: Check the File Size Before and After

ls -lh sample.txt.gz

You’ll see that the file is now smaller. Perfect for saving disk space!

Step 4: Decompress the File

If you want to get the original file back, you can decompress it using:

gunzip sample.txt.gz
ls
sample.txt

And yes, the file is back to its original form.

A Few Extra Tips

  • gzip -k filename keeps the original file intact
  • gzip -r foldername compresses all files in a folder recursively
  • gzip -l file.gz shows compression details

Compress and restore files with gzip

Today, you learned how to:

  • Create a test file
  • Compress it with gzip
  • Decompress it using gunzip
  • Use a few extra flags for advanced control

Keep practicing, and you’ll get comfortable with Linux in no time.

Try it, then check the worked answer

Your task: Compress a report while preserving the original.

Start in a fresh practice directory with gzip available. The first line creates the input report. We will use -c and redirection so the source remains in place, then decompress to terminal output rather than replace either saved file.

Bash · complete example for this question

printf 'Diya: 72\n' > marks.txt
gzip -c marks.txt > marks.txt.gz
gzip -dc marks.txt.gz

Expected result

Diya: 72

Worked answer

gzip -c marks.txt > marks.txt.gz

Choose gzip -c marks.txt > marks.txt.gz to preserve the source. The ordinary file form commonly replaces the input with its compressed version. Read the option's behavior before using it on a report for which you need both forms.

Check your decision

Compress a report while preserving the original.
Choose one answer. You can try again.

Answer and explanation

gzip -c marks.txt > marks.txt.gz

Choose gzip -c marks.txt > marks.txt.gz to preserve the source. The ordinary file form commonly replaces the input with its compressed version. Read the option's behavior before using it on a report for which you need both forms.

Choose explicitly whether the original should remain

Decide whether you want replacement or separate output before compressing. Use a matching decompressor to inspect the restored data, and remember that size savings depend on the input. Gzip compresses a stream; package a directory tree separately when you need many named entries collected together.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Compress with bzip2 →