Bird
Raised Fist0
Linux CLIscripting~5 mins

gzip and gunzip in Linux CLI - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes files take up too much space or are slow to send over the internet. gzip helps by squeezing files smaller. gunzip lets you open those squeezed files back to normal.
When you want to save space on your computer by making files smaller.
When you need to send files faster over the internet by compressing them first.
When you receive a compressed file and want to open it to see the contents.
When you want to archive logs or backups in a smaller size.
When you want to reduce download time for files on a website.
Commands
This command squeezes the file named example.txt to make it smaller. It replaces the original file with a compressed version called example.txt.gz.
Terminal
gzip example.txt
Expected OutputExpected
No output (command runs silently)
This command checks that the compressed file example.txt.gz exists and shows its size.
Terminal
ls -l example.txt.gz
Expected OutputExpected
-rw-r--r-- 1 user user 1234 Apr 27 12:00 example.txt.gz
→
-l - Shows detailed file information including size.
This command opens the squeezed file example.txt.gz and restores it back to the original example.txt file.
Terminal
gunzip example.txt.gz
Expected OutputExpected
No output (command runs silently)
This command confirms that the original file example.txt is back and shows its size.
Terminal
ls -l example.txt
Expected OutputExpected
-rw-r--r-- 1 user user 5678 Apr 27 12:01 example.txt
→
-l - Shows detailed file information including size.
Key Concept

If you remember nothing else from this pattern, remember: gzip makes files smaller by compressing them, and gunzip restores them back to normal.

Common Mistakes
Trying to gzip a file that is already compressed with .gz extension.
gzip will compress it again, which is usually unnecessary and wastes time.
Only gzip files that are not already compressed.
Using gunzip on a file that is not compressed or does not have a .gz extension.
gunzip will fail because it expects a compressed file format.
Only use gunzip on files ending with .gz that were compressed by gzip.
Expecting gzip to keep the original file after compression without flags.
By default, gzip deletes the original file after compressing it.
Use the -k flag with gzip to keep the original file if needed.
Summary
Use gzip filename to compress a file and save space.
Use gunzip filename.gz to decompress and restore the original file.
Check files with ls -l to see their sizes before and after compression.

Practice

(1/5)
1. What does the gzip command do in Linux?
easy
A. Lists files in a directory
B. Deletes a file permanently
C. Compresses a file and replaces the original with a .gz file
D. Creates a backup copy of a file

Solution

  1. Step 1: Understand gzip purpose

    The gzip command compresses files to save disk space.
  2. Step 2: Effect on original file

    It replaces the original file with a compressed file ending in .gz.
  3. Final Answer:

    Compresses a file and replaces the original with a .gz file -> Option C
  4. Quick Check:

    gzip compresses files = D [OK]
Hint: gzip compresses and replaces original file by default [OK]
Common Mistakes:
  • Thinking gzip deletes files without compression
  • Confusing gzip with backup commands
  • Assuming gzip lists files
2. Which of the following is the correct syntax to compress a file named data.txt but keep the original file?
easy
A. gzip data.txt
B. gzip -d data.txt
C. gunzip -k data.txt
D. gzip -k data.txt

Solution

  1. Step 1: Identify option to keep original

    The -k option tells gzip to keep the original file after compression.
  2. Step 2: Match correct command

    gzip -k data.txt compresses and keeps the original file.
  3. Final Answer:

    gzip -k data.txt -> Option D
  4. Quick Check:

    Use -k to keep original file [OK]
Hint: Use -k with gzip to keep original file [OK]
Common Mistakes:
  • Using gunzip instead of gzip for compression
  • Using -d which is for decompression
  • Not using -k and losing original file
3. What will be the output of the following commands?
gzip -k report.txt
gunzip report.txt.gz
medium
A. Only report.txt exists after commands
B. Both report.txt and report.txt.gz exist after commands
C. Original report.txt deleted, only report.txt.gz remains
D. Error because gunzip cannot decompress .gz files

Solution

  1. Step 1: Analyze gzip -k report.txt

    This compresses report.txt to report.txt.gz and keeps the original report.txt file.
  2. Step 2: Analyze gunzip report.txt.gz

    This decompresses report.txt.gz back to report.txt and deletes the .gz file.
  3. Final Answer:

    Only report.txt exists after commands -> Option A
  4. Quick Check:

    gzip -k keeps original, gunzip restores and removes .gz [OK]
Hint: gunzip removes .gz file after decompression [OK]
Common Mistakes:
  • Assuming gzip deletes original with -k option
  • Thinking gunzip keeps .gz file after decompression
  • Confusing gzip and gunzip roles
4. You run gunzip file.txt but get an error saying "not in gzip format." What is the likely problem?
medium
A. gunzip command is misspelled
B. file.txt is not a compressed .gz file
C. file.txt is already decompressed
D. You need to use gzip instead of gunzip

Solution

  1. Step 1: Understand gunzip input requirement

    gunzip expects a file compressed with gzip, usually ending with .gz.
  2. Step 2: Analyze error message

    "not in gzip format" means the file is not compressed or not a .gz file.
  3. Final Answer:

    file.txt is not a compressed .gz file -> Option B
  4. Quick Check:

    gunzip needs .gz files [OK]
Hint: gunzip only works on gzip-compressed (.gz) files [OK]
Common Mistakes:
  • Trying to decompress uncompressed files
  • Confusing gzip and gunzip commands
  • Ignoring file extensions
5. You have multiple log files: log1.txt, log2.txt, and log3.txt. You want to compress all of them but keep the original files. Which command will do this correctly?
hard
A. gzip -k log*.txt
B. gzip log*.txt
C. gunzip -k log*.txt
D. gzip -d log*.txt

Solution

  1. Step 1: Identify command to compress multiple files

    gzip compresses files; log*.txt matches all log files.
  2. Step 2: Ensure original files are kept

    The -k option keeps original files after compression.
  3. Final Answer:

    gzip -k log*.txt -> Option A
  4. Quick Check:

    gzip -k compresses and keeps originals [OK]
Hint: Use gzip -k with wildcard to keep originals [OK]
Common Mistakes:
  • Using gunzip instead of gzip for compression
  • Not using -k and losing original files
  • Using -d which decompresses files
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard