The quickest way to unzip a .gz file
Use the gunzip command followed by the filename. Open a terminal, navigate to the folder where your .gz file is stored, and type gunzip filename.gz. The command extracts the file and removes the .gz archive, leaving you with the uncompressed version in the same folder.
If you want to keep the original .gz file intact, use gunzip -c filename.gz > filename instead. This writes the uncompressed content to a new file without deleting the archive.
For files compressed as .tar.gz (a tar archive inside gzip compression), use tar -xzf filename.tar.gz. This single command unpacks both layers at once and creates the original directory structure or files.
Key Takeaways
- gunzip filename.gz uncompresses the file and removes the .gz archive in one step.
- gunzip -c filename.gz > filename uncompresses while keeping the original .gz file.
- tar -xzf filename.tar.gz handles both tar and gzip compression at the same time.
- You can unzip multiple files at once by listing them: gunzip file1.gz file2.gz file3.gz.
- Use zcat filename.gz to view the contents without creating a new file on disk.
Understanding .gz compression in Linux
A .gz file is compressed using the gzip algorithm, which reduces file size but keeps the original filename and modification date inside the archive. Linux treats .gz files as a standard format, and the tools to work with them come built into most distributions.
When you unzip a .gz file, you are reversing the compression process. The gunzip command reads the compressed data, reconstructs the original file, and writes it back to disk. This is different from a .zip archive, which can hold multiple files — a .gz file typically compresses a single file, though it is often paired with tar to bundle directories.
Using gunzip with different options
The gunzip command has several flags that change its behavior. The -c flag sends the uncompressed output to your terminal (stdout) instead of writing to a file, which is useful when you want to pipe the data to another command or save it with a different name.
The -k flag keeps the original .gz file after uncompressing. Type gunzip -k filename.gz and you will have both the compressed and uncompressed versions in the same folder. This is safer if you are unsure whether you need the archive later.
The -r flag works recursively on directories. If you have a folder full of .gz files, gunzip -r foldername uncompresses all of them at once. Add -k to keep the originals: gunzip -rk foldername.
Handling .tar.gz files
A .tar.gz file is a tar archive (which bundles multiple files and folders) that has been compressed with gzip. You could unzip it in two steps — first gunzip filename.tar.gz to get filename.tar, then tar -xf filename.tar to extract the contents — but tar can do both at once.
The command tar -xzf filename.tar.gz extracts everything in one step. The flags mean: -x extract, -z decompress gzip, -f read from a file. If you want to see what is inside before extracting, use tar -tzf filename.tar.gz to list the contents without unpacking.
Some older systems use tar -xzvf instead, where -v prints each filename as it extracts. Both work the same way; the -v just gives you progress feedback on larger archives.
Checking file integrity before unzipping
Before uncompressing a large .gz file, you can verify that it is not corrupted. Use gunzip -t filename.gz to test the archive without extracting it. If the file is intact, the command returns silently. If there is damage, it prints an error message.
You can also check the compression ratio and original file size without unzipping. The command gunzip -l filename.gz shows how much space the file takes compressed versus uncompressed, which is helpful if you are low on disk space and need to know whether you have room to extract.
Viewing .gz file contents without extracting
If you only need to read a .gz file without saving the uncompressed version, use zcat filename.gz. This command decompresses the file and sends it to your terminal, then discards it. For text files, you can pipe it to less for easier reading: zcat filename.gz | less.
The zgrep command searches inside a .gz file without uncompressing it. Type zgrep "search term" filename.gz and it will find and print matching lines. This saves disk space and time when you are looking for specific content in a large compressed file.
Troubleshooting common unzip problems
If you get a "command not found" error, gunzip is not installed. On Debian or Ubuntu systems, install it with sudo apt install gzip. On Red Hat or CentOS, use sudo yum install gzip. Most distributions include it by default, so this is rare.
If unzipping fails with a "not in gzip format" error, the file may be corrupted or not actually a .gz file despite its name. Check the file type with file filename.gz. If it says "gzip compressed data", the archive is valid but may be damaged. Try the test command gunzip -t filename.gz to see the specific error.
If you run out of disk space during extraction, gunzip stops and leaves the original .gz file intact. Free up space and try again. For very large files, check available space first with df -h to see how much room you have.
Frequently Asked Questions
Does gunzip delete the original .gz file?
Yes, by default gunzip removes the .gz file after uncompressing. If you want to keep it, use the -k flag: gunzip -k filename.gz. This creates the uncompressed file while leaving the archive in place.
Can I unzip a .gz file on Windows?
Windows 10 and later have built-in support for .gz files in File Explorer. Right-click the file and select "Extract All". On older Windows versions or for command-line access, install Windows Subsystem for Linux (WSL) or use a tool like 7-Zip or WinRAR.
What is the difference between gunzip and tar -xzf?
gunzip handles single-file gzip compression, while tar -xzf handles tar archives compressed with gzip. If your file is .tar.gz, use tar. If it is just .gz, use gunzip. Using the wrong command on the wrong file type will produce an error.
How do I unzip multiple .gz files at once?
List the filenames after gunzip: gunzip file1.gz file2.gz file3.gz. Or use a wildcard to unzip all .gz files in a folder: gunzip *.gz. Add -k to keep the originals: gunzip -k *.gz.
Can I see what is inside a .gz file before unzipping?
For .tar.gz files, use tar -tzf filename.tar.gz to list contents. For single-file .gz archives, use zcat filename.gz | head to view the first few lines, or zcat filename.gz | less to scroll through the entire file without extracting it.