How to Unzip a GZ File in Linux: Commands, Options, and What to Know First

If you've downloaded a .gz file on Linux and aren't sure what to do with it, you're not alone. GZ files are one of the most common compressed formats on Linux systems, and unpacking them is a core skill — whether you're extracting source code, log files, database backups, or software packages.

What Is a GZ File?

A .gz file is a compressed archive created using GNU Zip (gzip) compression. Gzip is a lossless compression algorithm, meaning no data is lost when you compress or decompress a file. It's widely used on Linux and Unix-based systems because it's fast, efficient, and built into virtually every distribution.

One important distinction: a .gz file typically compresses a single file. If someone bundled multiple files together first, they likely used tar before gzip — producing a .tar.gz (or .tgz) file. These two cases require slightly different commands, which is where most confusion begins.

The Basic Command: gunzip

The simplest way to decompress a .gz file is with the gunzip command:

gunzip filename.gz 

This decompresses the file in place, replacing filename.gz with the original uncompressed file. The compressed version is deleted by default.

If you want to keep the original .gz file intact after decompression, add the -k flag:

gunzip -k filename.gz 

Using gzip -d as an Alternative

gunzip is actually a shorthand for gzip -d (decompress mode). These two commands are functionally identical:

gzip -d filename.gz 

Use whichever feels more intuitive — the result is the same.

Decompressing .tar.gz Files 🗂️

For .tar.gz files (compressed archives containing multiple files or directories), the standard tool is tar with the right flags:

tar -xzf filename.tar.gz 

Breaking down those flags:

FlagMeaning
-xExtract files
-zDecompress using gzip
-fSpecifies the filename to operate on

To extract into a specific directory rather than the current one:

tar -xzf filename.tar.gz -C /path/to/destination 

To preview the contents without extracting:

tar -tzf filename.tar.gz 

This lists every file inside the archive — useful before you extract, especially with unfamiliar downloads.

Decompressing Without Deleting the Original

By default, gunzip removes the .gz file after extraction. If that's a problem — say, you're working with a backup you need to preserve — your options are:

  • Use gunzip -k (keep original)
  • Use zcat to decompress and redirect output to a new file:
zcat filename.gz > output_filename 

zcat is particularly useful in pipelines, where you want to read compressed content without writing an intermediate uncompressed file to disk.

Decompressing Multiple GZ Files at Once

If you have a directory full of .gz files to unpack, you can use a wildcard:

gunzip *.gz 

Or loop through them with a shell script for more control:

for f in *.gz; do gunzip "$f"; done 

This processes each file individually and is safer than the wildcard approach when filenames contain spaces or special characters.

Checking Integrity Before Decompressing

If you're working with downloaded files or backups, it's worth verifying the file isn't corrupted before extracting:

gunzip -t filename.gz 

The -t flag tests the file's integrity without actually decompressing it. If the file is intact, the command returns silently. If it's corrupted, you'll see an error — better to know before you need the data.

Variables That Affect Which Approach Works Best 🔧

Not every situation calls for the same command. A few factors shape which method fits:

  • File type: A plain .gz file and a .tar.gz archive need different tools (gunzip vs. tar)
  • Whether the original needs preserving: Log rotation scripts, backup pipelines, and automated workflows often require the source file to remain untouched
  • Destination directory: Extracting to the current directory vs. a specific path changes the command
  • Scale: Decompressing one file by hand is different from scripting bulk extraction across hundreds of files
  • Piping into other commands: zcat shines in pipelines where you want to process compressed data on the fly — for example, grepping through a compressed log without fully writing it out

What About GUI Tools?

Most Linux desktop environments include a file manager that handles .gz and .tar.gz files with a right-click. GNOME's Files (Nautilus) and KDE's Dolphin both support extraction without touching the terminal. These work well for occasional, manual decompression — but offer less control over output paths, file preservation, or batch processing.

Command-line tools remain the standard for scripting, server environments, and any workflow where consistency and automation matter.

A Note on Similar Formats

Linux users frequently encounter related formats that look similar but aren't identical:

FormatTool to Decompress
.gzgunzip or gzip -d
.tar.gz / .tgztar -xzf
.bz2bunzip2 or tar -xjf
.xzunxz or tar -xJf
.zipunzip

Gzip is just one member of a larger compression ecosystem on Linux. The right tool depends on what format you're actually dealing with — which is worth confirming before running any command.

How straightforward this process turns out to be depends heavily on your specific situation: whether you're working interactively or in a script, on a desktop or a headless server, with a single file or a structured archive, and how much control you need over where things land and whether originals are preserved.