UNIX / Linux Decompress tgz / tar.gz and extract files

I am a new Linux system user. I have downloaded a file from the internet in .tgz format. How do I decompress .tgz file under UNIX or Linux operating system using a shell prompt? How can I decompress .tar.gz file on Unix?

Introduction – A tarball is nothing but a group of files that are kept together using the tar command. Typically have .tar extension. Compress .tar file have .tgz or .tar.gz extension. You get the tar file is compressed when compressed using gzip and other Linux or Unix applications. So tgz archive file also known as tar ball. If an archive is compressed with an external tool, the compression program adds its own suffix as usual, resulting in filename endings like “.tar.Z”, “.tar.gz”, and “.tar.bz2” or tgz. This page shows how to extract .tar.gz/.tgz files using Linux or Unix command line.

Linux decompress tgz file

To open such file type the following tar command at Linux shell prompt (GNU tar syntax):
$ tar -zxvf filename.tgz
Linux Decompress tgz file command
The above, command will extract the .tgz file into the current directory. To view files use the cd command and ls command:
ls -l
cd dir1

Unix decompress .tar.gz file

Let us see how to extract .tar.gz files using Unix or Linux command line, run:
$ tar -zxvf filename.tar.gz

How to decompressing .tar.bz2 file

Run the following command to extract .tar.bz2 file on Unix or Linux:
$ tar -jxvf filename.tar.bz2

How to uncompress tar.gz/tgz/tar.bz2 file into a different directory

The syntax is:
tar -zxvf filename.tgz -C /path/to/dir1/
tar -zxvf filename.tar.gz /dir2/
tar -jxvf filename.tar.bz2 /path/to/dir3

How to to extract single file named foo.mp4 from the backup.tar.gz

tar -xvzf backup.tar.gz foo.mp4
Extract dir2 from backup.tar.gz, run:
tar -xvzf backup.tar.gz dir2

How to view a listing all files in tarball archive

Again use the tar command as follows to see a listing of all files in backup.tar.gz:
tar -ztvf backup.tar.gz

UNIX decompress tgz / tar.gz file

Type the following command:
$ gzip -dc filename.tgz | tar xf -
$ gzip -dc filename.tar.gz | tar xf -

If file extension ends with .Z, enter:
$ zcat filename.tar.Z | tar xf -

Summary of tar command options

  • z – Decompress/extract tar.gz or .tgz file
  • j – Decompress/extract tar.bz2 or .tbz2 file
  • x – Extract files
  • v – Verbose output on screen
  • t – List files stored inside given tarball archive
  • f – Extract given filename.tar.gz and so on


This page showed how to extract (decompress) tar.gz/tgz files using Linux and Unix command line options.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 5 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • Rani Dec 21, 2007 @ 20:25

    How do I extract .tar.gz files using mac OS X terminal app?

    • 🐧 Vivek Gite Dec 22, 2007 @ 13:26

      Open the Terminal app and type. Use cd to change the dir where foo.tar.gz stored. For example, go to ~/Desktop/
      cd ~/Desktop/
      ls -l foo.tar.gz
      tar -zxvf foo.tar.gz

  • Soothe user May 1, 2008 @ 1:02

    A .tar.gz file is an archive in compressed format.

    Extract file.tar
    tar -xvf file.tar

    Extract file.tar.gz
    tar -zxvf file.tar

    Extract file.tar.bz2
    tar -jxvf file.tar

    Replace x with t to list files:
    tar -ztvf file.tar
    tar -jtvf file.tar

  • Sayali Aug 14, 2009 @ 12:13

    Command to unzip/extract a .tar.gz file for me was:
    tar -xvzf mysql-backup-data.tar.gz
    tar -xvzf mysql-backup-data.tar.gz -C /data/mysql

    man tar has for information

  • PS Sep 29, 2020 @ 16:44

    How do you re-compress the extracted files to the same zip .tgz file in the same place exactly left- (once you extract its impolite to leave the cleanup to someone else)

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum