Extract or Unzip Tar Gz (.tar.gz, tgz) File in Linux

A TAR file is an archive file that collects and combines several files into one manageable file. This is usually for the purposes of distributing or archiving, and the name comes from “Tape ARchive”.

In this tutorial, we will learn how to extract tar.gz and tgz archives.

Table of Contents

  1. Extract File
  2. Extract To Specific Directory
  3. Extract Specific Files

Extract File

By default, most Linux distributions come with the pre-installed tar command. To extract a tar.gz file, run the below command:

tar -xf archive.tar.gz

Here, -x is --extract option and f option to specify the archive file name.

During extracting, if you want to print the names of the files use -v option.

tar -xvf archive.tar.gz

Extract To Specific Directory

Tar extracts the archive contents in the current folder by default. If you want to extract to a specific location, then use the -C (--directory) to extract archive files in a specific directory.

tar -xf archive.tar.gz -C /home/shouts/files

Extract Specific Files

To extract a specific file/s from the tar file, write a space-separated list of file/folder names. Specific files:

tar -xf archive.tar.gz dir1 dir2

Specific files:

tar -xf archive.tar.gz file1 file2