Learning how to unzip gz file linux is a basic skill for anyone working with compressed archives on the command line. Extracting gz files on Linux involves recognizing that gzip alone compresses single files, not directories. This guide will walk you through every method you need, from simple commands to advanced options.
You will often encounter .gz files when downloading software or transferring logs. The gzip tool is fast and efficient, but it works differently than zip or tar. Let’s clear up the confusion and get you extracting files in minutes.
Understanding Gzip Compression On Linux
Gzip, short for GNU zip, is a compression utility. It replaces the original file with a compressed version ending in .gz. Unlike Windows zip, gzip does not bundle multiple files into one archive by itself.
When you compress a single file with gzip, the original file disapears. You are left with only the .gz file. To unzip it, you reverse this process.
Gzip Vs Tar: What Is The Difference
Many beginners confuse gzip with tar. Tar bundles files into a single archive, but does not compress them. Gzip compresses a single file. Combined, they create .tar.gz or .tgz files.
If you see file.tar.gz, you need to extract the tar archive first, then decompress. But for a plain .gz file, you only need the gzip tools.
How To Unzip Gz File Linux Using Gunzip
The simplest way is using the gunzip command. It is a direct wrapper around gzip that decompresses files. Here is the basic syntax:
gunzip filename.gz
This command removes the .gz file and leaves you with the original uncompressed file. For example, gunzip data.txt.gz gives you data.txt.
Step-By-Step Example With Gunzip
- Open your terminal.
- Navigate to the directory containing the
.gzfile usingcd. - Type
gunzip file.gzand press Enter. - Verify the output with
ls -l. The.gzfile should be gone.
This method works for any single compressed file. It is fast and requires no extra flags.
Keeping The Original Gz File After Extraction
By default, gunzip deletes the compressed file. If you want to keep it, use the -k (keep) option:
gunzip -k filename.gz
Now you have both filename.gz and the decompressed file. This is usefull when you need the original archive for backup.
How To Unzip Gz File Linux Using Gzip -D
The gzip command itself can decompress files with the -d flag. It works identically to gunzip. Use it like this:
gzip -d filename.gz
You can also use the longer form --decompress. Both do the same thing. Many users prefer gzip -d because it is one less command to remember.
Practical Example With Gzip -D
Imagine you have a log file named server.log.gz. Run:
gzip -d server.log.gz
The output is server.log. Check the file size with wc -l server.log to see the content.
If you want verbose output showing compression ratios, add the -v flag: gzip -dv file.gz.
Extracting Tar.Gz Files: A Common Variation
Most Linux archives are .tar.gz or .tgz. These are not plain gz files. They are tar archives compressed with gzip. To extract them, use tar directly.
The command is:
tar -xzf archive.tar.gz
Here is what each flag means:
-x: Extract-z: Decompress with gzip-f: Use the following file
You can also use tar -xzf file.tgz for .tgz files.
Listing Contents Before Extracting
To see what is inside a .tar.gz file without extracting, use:
tar -tzf archive.tar.gz
This lists all files and folders. It helps you avoid extracting unwanted files into your current directory.
Using Zcat To View Gz Files Without Extracting
Sometimes you only need to read the content of a compressed file. The zcat command prints the decompressed content to the terminal without creating a file.
zcat file.gz
You can pipe this to less for easier reading:
zcat file.gz | less
This is perfect for checking logs or configuration files quickly. It does not modify your filesystem.
Zcat With Grep
Search for a pattern inside a gz file without extracting:
zcat file.gz | grep "error"
This command is a huge time saver when debugging. You avoid creating temporary files.
How To Unzip Gz File Linux With Zless And Zmore
Two other utilities, zless and zmore, let you page through compressed files. They are similar to less and more but work directly on gz files.
Use zless file.gz to scroll through the content. Press q to quit. Use zmore file.gz for a simpler pager.
These commands do not decompress the file to disk. They stream the data to your terminal.
Extracting Multiple Gz Files At Once
If you have many .gz files in a directory, you can decompress them all with a single command. Use a wildcard:
gunzip *.gz
This decompresses every file ending in .gz in the current directory. Be careful: it will process all matching files.
To keep the original files, add the -k flag:
gunzip -k *.gz
Using Find With Gunzip
For nested directories, combine find with gunzip:
find . -name "*.gz" -exec gunzip {} \;
This finds all .gz files under the current directory and decompresses them. It is powerfull but can be dangerous if you run it in the wrong location.
Handling Corrupted Gz Files
Sometimes a .gz file is damaged or incomplete. The gzip -t command tests the integrity without extracting:
gzip -t file.gz
If the file is valid, you see no output. If it is corrupted, you get an error message. You can then try to recover data with zcat or use dd to salvage partial content.
Forcing Extraction Of A Corrupted File
Use gzip -df to force decompression even if the file appears damaged:
gzip -df file.gz
This may produce partial data. It is not a guarantee, but it can save you in a pinch.
How To Unzip Gz File Linux Without Losing Permissions
File permissions are preserved automatically when using gunzip or gzip -d. The decompressed file retains the original owner, group, and mode bits.
However, if you use zcat and redirect output, permissions are lost. For example:
zcat file.gz > newfile
This creates newfile with default permissions. To preserve them, stick with gunzip or gzip -d.
Advanced: Using Pv To Monitor Extraction Progress
For large .gz files, you might want to see progress. Install pv (Pipe Viewer) and use:
pv file.gz | gunzip > outputfile
This shows a progress bar and transfer speed. It is not installed by default on most systems, but it is available in package managers.
Installing Pv On Ubuntu Or Debian
sudo apt install pv
On CentOS or Fedora:
sudo yum install pv
Then use it as shown above. It makes long extractions less boring.
Common Mistakes When Unzipping Gz Files
Beginners often try to use unzip on .gz files. That command is for .zip archives only. It will fail with an error.
Another mistake is forgetting the -z flag with tar. If you run tar -xf archive.tar.gz without -z, tar may still work on some systems, but it is not reliable.
Also, do not try to decompress a .tar.gz file with gunzip alone. It will give you a .tar file, not the original files. You then need to extract the tar archive separately.
How To Unzip Gz File Linux On Different Distributions
The commands work the same on Ubuntu, Debian, Fedora, CentOS, Arch, and most others. Gzip is part of the GNU coreutils, so it is preinstalled everywhere.
If you get a “command not found” error, install gzip:
- Debian/Ubuntu:
sudo apt install gzip - Red Hat/Fedora:
sudo yum install gzip - Arch:
sudo pacman -S gzip
But this is rare. Gzip is almost always present by default.
Automating Extraction With Scripts
You can write a simple bash script to extract many files. Save this as extract_gz.sh:
#!/bin/bash
for file in *.gz; do
gunzip -k "$file"
done
Make it executable with chmod +x extract_gz.sh and run it. This keeps the original files and creates decompressed copies.
Extracting And Moving In One Step
To extract a file to a specific directory:
gunzip -c file.gz > /target/directory/filename
The -c flag writes to stdout. You redirect it to the desired location. This avoids leaving the decompressed file in the current folder.
Performance Tips For Large Gz Files
Gzip decompression is CPU-bound. On multi-core systems, you can use pigz (parallel gzip) for faster decompression. Install it with your package manager.
To decompress with pigz:
pigz -d file.gz
Pigz uses all available cores. It is not always faster for small files, but for multi-gigabyte archives, it makes a big difference.
Checking Compression Ratio
Use gzip -l file.gz to see the compression ratio and original size:
gzip -l file.gz
This shows the compressed size, uncompressed size, and ratio. It helps you estimate space requirements before extracting.
How To Unzip Gz File Linux On Embedded Systems
On routers or minimal Linux systems, you may have BusyBox instead of full gzip. The commands are the same: gunzip and gzip -d work identically.
If space is limited, use zcat to avoid writing the decompressed file to disk. This is common on OpenWrt or Alpine Linux.
Security Considerations
Always verify the source of a .gz file before extracting. Malicious archives can contain symlinks that overwrite system files. Use tar -tzf to list contents first.
For .tar.gz files, extract in a temporary directory:
mkdir /tmp/extract
cd /tmp/extract
tar -xzf /path/to/archive.tar.gz
This contains any potential damage. After inspection, move the files where you need them.
Frequently Asked Questions
What is the command to unzip a gz file in Linux?
Use gunzip filename.gz or gzip -d filename.gz. Both decompress the file and remove the .gz extension.
How do I unzip a tar.gz file in Linux?
Run tar -xzf archive.tar.gz. The -z flag tells tar to decompress with gzip before extracting.
Can I unzip a gz file without removing the original?
Yes, use gunzip -k filename.gz or gzip -dk filename.gz. The -k flag keeps the compressed file.
What is the difference between gzip and gunzip?
Gzip compresses files. Gunzip decompresses them. They are the same program; gunzip is a symlink to gzip with default decompression behavior.
How do I view the contents of a gz file without extracting?
Use zcat file.gz to print to the terminal, or zless file.gz to page through it. These commands do not create a decompressed file.
Conclusion
Now you know how to unzip gz file linux using multiple methods. Start with gunzip for simple files, use tar -xzf for archives, and try zcat for quick peeks. Practice these commands on sample files to build muscle memory.
Linux gives you many ways to handle compressed data. Choose the one that fits your workflow. With these tools, you will never be stuck by a .gz file again.