Unpacking or uncompressing gz files under Linux and UNIX systems

See all UNIX related articles/faq
How do I open or uncompress .gz extension files under Linux or UNIX computers? What command do I need to type to decompress a file named archive.gz under Linux?

The .gz file extension is created using Gzip or gunzip program, which reduces the size of the files using Lempel-Ziv coding (LZ77) algorithm. Your Linux or Unix system comes with gunzip/gzip software command-line application which is used for file compression. The gzip is short for GNU zip. The program is a free software replacement for the compress program used in early Unix systems. This page explains how to unpack or decompress/uncompress .gz file under Linux or Unix-like systems such as macOS, FreeBSD and others.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix
Est. reading time 3 minutes

Decompressing gz files

The procedure is as follows for Linux, macOS and Unix users:

  1. Open the terminal application.
  2. For remote systems use the ssh command for log in purposes
  3. To decompress .gz files, use:
    gzip -d filename.gz
  4. One can unzip and open gz file using:
    gunzip archive.gz
  5. For .tar.gz/.tgz file try the tar command:
    tar -xvf archive.tar.gz
  6. Run the ls command to list the files.

Let us see all commands and examples in details.

How to unpack or uncopress gz files under Linux and UNIX systems

Each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times. To expand i.e. for unpacking or uncompressing gz files you need to use gunzip program. You can also use the gzip -d or zcat program:

Using the gunzip command for unpacking .gz files

The gunzip syntax is as follows:
$ gunzip file.gz
$ gunzip archive.z

In other words, it takes a list of files on its command line and replaces each file whose name ends with .gz, -gz, .z, -z, or _z (ignoring case) and which begins with the correct magic number with an uncompressed file without the original extension.
$ gunzip file.gz # uncomrpess file.gz
$ ls file # list it

How to Unzip (Open) Gz File in Linux and Unix

Using the gzip command for decompress a file

The gzip command syntax is simple too:
$ gzip -d foo.gz # <--decompress foo.gz
$ ls file # <--verify it

You can also use the following syntax with the gzip to decompress file.gz:
$ gzip --decompress filename1.gz
$ gzip --uncompress file2.gz

How to Unzip and Open Gz File on Linux or Unix
Please note that gunzip can currently decompress files created by gzip, zip, compress, or pack Unix programs.

Keep the file when unpacking

You can keep input files during compression or decompression. In other words, don’t delete the file by passing the -k options to the gzip:
$ gzip -d -k file.gz
$ ls -l file*

Getting verbose output while uncompressing gz file

Want to display the name and percentage reduction for each file compressed or decompressed? Pass the -v option:
$ gzip -v -d -k file2.gz
$ ls -l file2*

Because of the -k option original .gz file is not deleted on my system:
Unpacking or uncompressing gz files under Linux and UNIX systems

How do I deal with a .tar.gz file?

If file extension is tar.gz, then type the tar command as follows:
$ tar -zxvf filename.tar.gz
All files would be extracted in the current working directory. Another syntax is:
gunzip < filename.tar.gz | tar xvf -

How to unzip and open gz or tar.gz file in different directory

For tar.gz, try passing the -C option:
tar -xvf filename.tar.gz -C /path/to/dest
tar -xvf filename.tar.gz -C /tmp/

Summing up

I hope you understood how to decompress or uncompress .gz or tar.gz files using the gzip, gunzip or tar command on macOS, Linux and Unix-like systems. For more info please read documentation using the man command or help command:
man gzip
man tar

See also:

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

6 comments… add one
  • Baby Fox Jun 5, 2007 @ 13:32

    For .tgz files, I type:

    gunzip < my-file-name.tgz | tar xvf -
    ls -l
  • Garbage Collector Jun 13, 2009 @ 6:42

    It is simple and here is how to open or unzip .gz files on RHEL server.

    1. Unzipping gz File:
      gzip -d name.gz
    2. Extracting tar.gz File:
      tar -xf name.tar.gz
    3. Stuck with something? Just do:
      man gzip
      man tar
  • Robert Romanov Jul 21, 2010 @ 18:25

    I down loaded the file gzip-1.4.tar.xz into a directory /usr/compiler
    What RedHat linux command do I use to uncompress it?

  • V K Prasad Oct 23, 2011 @ 11:33

    I was wondering how to decompress files in gzip format and this page helped me so much. I moved from Windows to Linux so this is all new on the AWS server.

  • lies Oct 4, 2012 @ 9:56

    u can use a cat :} nyan

  • Anonymous Feb 4, 2022 @ 13:52

    Thank you, this was helpful

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.