How to Unzip a .gz File in Linux

By 

Updated on

4 min read

GZ file icon beside the text unzip .gz file on a yellow background

Sooner or later, you will run into a .gz file, such as a rotated log, downloaded data file, or database dump. Gzip compresses a single data stream, so a raw .gz file is a compressed file rather than a multi-file archive.

This article explains how to decompress .gz files, keep the original file when needed, view compressed text without extracting it, and handle .tar.gz archives. The unzip command handles ZIP archives and does not decompress .gz files.

Unzipping a .gz File

On Linux and macOS, use the built-in gzip utility to decompress a .gz file. To restore the original file and remove the .gz extension, run:

Terminal
gzip -d file.gz

The command writes the decompressed data to file and removes file.gz after successful decompression.

Keep the Original .gz File

If you want to decompress the file without deleting the original compressed file, use the -k option:

Terminal
gzip -dk file.gz

You can also keep the original file by writing the decompressed output somewhere else with the -c option, which sends the result to standard output:

Terminal
gzip -dc file.gz > decompressed-file

Choose a destination that does not already exist. Shell redirection truncates an existing file before gzip runs, so check the path first. For sensitive data, write to a private directory rather than a shared /tmp directory.

Using gunzip

Another command you can use to decompress a .gz file is gunzip . This command is a convenience alias for gzip -d.

To open a .gz file with gunzip, run:

Terminal
gunzip file.gz

The command produces the uncompressed file and removes the .gz compressed file by default.

Viewing a gz File Without Extracting

Sometimes you only need to look inside a compressed text file, such as a rotated log, without creating an uncompressed copy on disk. The zcat, zless, and zgrep commands print, page through, and search gzip-compressed text directly.

To print the contents of a .gz file to the terminal, use zcat:

Terminal
zcat file.gz

To scroll through a large file page by page, use zless:

Terminal
zless file.gz

You can also search a compressed file with zgrep:

Terminal
zgrep "pattern" file.gz

zgrep accepts many common grep options, including -i for case-insensitive searches and -n to show line numbers. It does not support recursive options such as -r or -R, or file-selection options such as --include and --exclude.

None of these commands modify or remove the original .gz file.

Extracting .gz Files Using a File Manager

If you use a desktop environment and are not comfortable with the command line, you can use your file manager to extract .gz files.

To open (unzip) a .gz file, right-click on the file you want to decompress and select “Extract” (or a similar option, depending on your file manager).

On Windows 11, try File Explorer first. If it does not offer an extraction option for a .gz file, use a tool such as 7-Zip .

Extracting a .tar.gz File

Gzip compresses one data stream and does not store file names or directory structures for multiple files.

The tar format combines multiple files and directories into one archive, which gzip can then compress. Files ending in .tar.gz or .tgz are tar archives compressed with gzip. Before extracting, list the archive contents and check the stored paths:

Terminal
tar -tf archive.tar.gz

After checking the list, create a destination directory and extract the archive into it:

Terminal
mkdir archive-contents && tar -xf archive.tar.gz -C archive-contents

Modern versions of tar detect gzip compression automatically. For more options, see our guide on extracting tar.gz files .

Troubleshooting

gzip: file.gz: not in gzip format
The file is not actually gzip compressed, even if its name ends in .gz. This often happens when a download was renamed or the file is a plain tar archive. Run file file.gz to check the real format before extracting.

gzip: file.gz: unexpected end of file
The compressed file is truncated, usually because a download was interrupted. Confirm the error with gzip -t file.gz, then download or copy the file again.

file already exists; do you wish to overwrite prompt
A file with the target name is already in the directory. Inspect, rename, or back up the existing file before proceeding. Use -f only when you intend to replace it because the overwritten contents cannot be recovered with gzip.

Conclusion

Use gzip -d or gunzip for a raw .gz file, and use tar -xf for a .tar.gz archive after checking its contents. Keep the gzip cheatsheet nearby for additional commands and options.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page