What a .gz file is and why Linux handles it differently

A .gz file is a compressed archive — a single file that contains one or more other files squeezed down to take up less space. Linux treats it as a regular file you can work with from the command line, the same way you'd navigate folders or rename documents. You don't need special software or a graphical interface. The tools to open and extract .gz files come built into Linux itself.

The compression method is called gzip, and it's been part of Unix and Linux systems for decades. When you read a .gz file on Linux, your computer doesn't automatically unpack it the way Windows might unzip a folder. You have to tell it to decompress the file using a command.

Key Takeaways

  • Use gunzip filename.gz to decompress a .gz file and replace it with the uncompressed version in the same location.
  • Use gzip -d filename.gz to do the same thing — both commands work identically.
  • Use zcat filename.gz to read the contents without creating a new file, useful when you just want to peek inside.
  • Use gzip -dk filename.gz to decompress and keep the original .gz file intact.
  • If the file is a .tar.gz (a compressed folder), use tar -xzf filename.tar.gz to extract everything at once.

The simplest way: gunzip command

Open your terminal and navigate to the folder where your .gz file is stored. Type gunzip filename.gz, replacing "filename" with the actual name of your file. Press Enter.

The command runs when ready (or within seconds for large files). When it finishes, the original .gz file disappears and you're left with the uncompressed version. If the file was called document.txt.gz, you'll now have document.txt in the same folder.

Keeping the original file: the -k flag

By default, gunzip deletes the compressed file once it finishes. If you want to keep both the .gz file and the uncompressed version, add the -k flag: gunzip -k filename.gz.

This is useful when you're not sure whether you'll need the compressed version again, or when you're working with a file you downloaded and want to preserve the original read.

Reading a .gz file without extracting it

Sometimes you just want to see what's inside without creating a new file. Use zcat filename.gz to display the contents directly in your terminal. The original .gz file stays untouched.

This works well for text files — logs, configuration files, or documents. For binary files (images, executables), the output will be unreadable characters, but the command still works. You can pipe the output to other commands too: zcat filename.gz | grep "search term" will search for text inside the compressed file without extracting it.

When your file is .tar.gz (a compressed folder)

A .tar.gz file is different from a plain .gz file. It's a folder that's been packaged into a single archive and then compressed. You can't just use gunzip on it — you need the tar command instead.

Type tar -xzf filename.tar.gz. The flags mean: -x (extract), -z (decompress gzip), -f (from this file). This command unpacks the entire folder structure and all its contents into your current directory. The original .tar.gz file remains in place.

Troubleshooting: file not found or permission denied

If you get "file not found", check that you're in the right folder. Type ls to list all files in your current location and confirm the filename matches exactly — Linux is case-sensitive, so Document.gz and document.gz are different files.

If you get "permission denied", the file exists but you don't have permission to modify it. Type ls -l filename.gz to see the permissions. If the file is owned by another user or marked read-only, you may need to ask the owner for access or use sudo (though this is rarely necessary for straightforward decompression). In most cases, you can read and extract files you've downloaded yourself without any permission issues.

Comparing your options: which command to use

What you want to doCommandResult
Extract .gz file, remove the originalgunzip filename.gzUncompressed file remains; .gz is deleted
Extract .gz file, keep the originalgunzip -k filename.gzBoth files remain
Read contents without extractingzcat filename.gzContents display in terminal; .gz unchanged
Extract a .tar.gz foldertar -xzf filename.tar.gzFolder and contents extracted; .tar.gz unchanged

Frequently Asked Questions

Can I open a .gz file in a text editor?

Not directly — the file is compressed binary data, so it will show as gibberish. You must decompress it first using gunzip or zcat, then open the uncompressed file in your editor. Some advanced text editors can read .gz files directly, but the command line is faster and more reliable.

What's the difference between gunzip and gzip -d?

They do exactly the same thing. gunzip is a shorthand command, while gzip -d uses the gzip command with the decompress flag. Both delete the original .gz file by default. Use whichever you find easier to remember.

Why does my .tar.gz file need a different command?

A .tar.gz is two operations stacked together: tar packages multiple files into one archive, then gzip compresses it. You need tar to unpack the archive structure and gzip to decompress at the same time. The -z flag in tar handles the gzip part automatically.

Will decompressing a .gz file take a long time?

Usually no — decompression is fast, even for large files. A file that takes seconds to read might decompress in under a second. If decompression seems stuck, check your disk space; if the drive is full, the command will hang.

Can I compress files back into .gz format?

Yes, using gzip filename. This compresses the file and replaces it with a .gz version. Use gzip -k filename to keep the original. For folders, use tar -czf archive.tar.gz folder-name to create a .tar.gz archive.