The basic command to extract a .gz file

To unzip a .gz file in Linux, use the gunzip command followed by the filename. Open your terminal, navigate to the folder containing the file, and type gunzip filename.gz. The command will extract the file and remove the .gz archive by default, leaving you with the uncompressed version.

If the file is named document.txt.gz, running gunzip document.txt.gz will create document.txt in the same folder. The original .gz file disappears after extraction unless you use the -k flag to keep it.

You can also use gzip -d filename.gz to do the same thing — both commands work identically. The -d flag tells gzip to decompress instead of compress.

Key Takeaways

  • Use gunzip filename.gz to extract a .gz file; the original archive is deleted by default.
  • Add the -k flag to keep the original file: gunzip -k filename.gz.
  • For files compressed as tar.gz archives, use tar -xzf filename.tar.gz instead to preserve the folder structure.
  • Check what you are extracting first by listing contents with gunzip -l filename.gz or tar -tzf filename.tar.gz.

Keeping the original .gz file after extraction

By default, gunzip deletes the compressed file once extraction is complete. If you want to keep both the .gz archive and the extracted file, use the -k flag: gunzip -k filename.gz. This leaves the original untouched.

This is useful when you need to send the compressed file to someone else or store a backup. Without the -k flag, you will only have the uncompressed version afterward.

Extracting tar.gz files (compressed archives with folders)

A .tar.gz file is different from a plain .gz file — it is a folder or multiple files compressed together. Using gunzip on a .tar.gz file will only remove the .gz layer, leaving you with a .tar file that still needs to be unpacked.

Instead, use tar -xzf filename.tar.gz to extract the entire archive in one step. The flags mean: -x (extract), -z (decompress gzip), and -f (read from file). This command preserves the original folder structure inside the archive.

If you have already extracted the .tar layer and have a .tar file left over, use tar -xf filename.tar to unpack it.

Checking what is inside before extracting

Before extracting a large .gz file, you may want to see what is inside. Use gunzip -l filename.gz to list the contents without extracting. This shows the uncompressed size, compressed size, and the original filename.

For .tar.gz files, use tar -tzf filename.tar.gz to list all files and folders inside the archive. The -t flag lists contents, -z handles gzip decompression, and -f reads from the file. This is especially helpful when you want to know the folder structure before extracting.

Extracting to a different folder

By default, gunzip extracts files to the current directory. To extract to a different folder, use gunzip -c filename.gz > /path/to/folder/filename. The -c flag sends the output to standard output instead of writing directly to disk, and the > redirects it to your chosen location.

For .tar.gz files, use tar -xzf filename.tar.gz -C /path/to/folder. The -C flag tells tar to change to that directory before extracting. This is cleaner than using redirection and preserves the internal folder structure.

Handling multiple .gz files at once

If you have several .gz files in the same folder, you can extract them all with one command: gunzip *.gz. The asterisk is a wildcard that matches all files ending in .gz. Each file will be extracted and the original archives will be deleted.

To keep the originals, use gunzip -k *.gz. To extract to a different folder, you will need to use a loop: for file in *.gz; do gunzip -c "$file" > /path/to/folder/"${file%.gz}"; done. This extracts each file and redirects it to your chosen location while removing the .gz extension from the filename.

Troubleshooting common extraction problems

If you get a "No such file or directory" error, make sure you are in the correct folder or provide the full path to the file: gunzip /home/username/Downloads/filename.gz. Use pwd to check your current location and ls to list files in the folder.

If extraction fails with a "not in gzip format" error, the file may be corrupted or not actually a .gz file. Check the file type with file filename.gz — it should say "gzip compressed data". If it says something else, the file may need a different tool to extract it.

Permission errors mean you do not have write access to the folder. Either use sudo (if you are the administrator) or extract to a folder where you have permission, like your home directory.

Frequently Asked Questions

What is the difference between gunzip and gzip -d?

They do the same thing — gunzip filename.gz and gzip -d filename.gz both decompress the file. gunzip is simpler to type and is the standard command for extraction. Use whichever you prefer; the result is identical.

Can I extract a .gz file without deleting the original?

Yes, use gunzip -k filename.gz to keep the original file. The -k flag stands for "keep" and leaves the .gz archive untouched after extraction.

Why does tar -xzf work but gunzip does not on my .tar.gz file?

Because .tar.gz files contain multiple files or folders compressed together. gunzip only removes the gzip layer, leaving a .tar file that still needs unpacking. tar -xzf handles both steps at once and is the correct tool for .tar.gz archives.

How do I know if a file is .gz or .tar.gz?

Check the filename — .tar.gz files have both extensions, while plain .gz files have only .gz. You can also use file filename to see the file type. If it says "tar archive", use tar; if it just says "gzip compressed data", use gunzip.

What if I accidentally deleted the .gz file after extraction?

The extracted file remains — you still have the uncompressed version. If you need the .gz file again, you can recreate it by compressing the extracted file with gzip filename. This creates a new .gz archive from the uncompressed file.