How to extract tar.gz file in Linux

Overview

The topic for this post will be how to extract tar.gz file in Linux using the tar tool in terminal. In addition we’ll showcase other basic tar options too. If you are new to Linux OS, you must be heard of the tar files. Most open-source packages are distributed in tar format. Every Linux user should be familiar with the the tar command, at least the basics.

The tar is the most wildly used archive package in Linux OS, just like zip files. Most Linux distributions used tar by default. A tar archive is pre-installed on many Linux Operating Systems, and it stands for “Tape Archive” every so often it is also referred to as the tarball.

In this tutorial, we will show you how to create and extract tar.gz file in Linux using the terminal.

The tar command Syntax:

The basic option and syntax of the tar command are as shown the below:

tar [option] [file_name.tar.gz]

Create Tar archive

You can create a tar archive from files or folders

Syntax:

tar -cvzf file_name.tar.gz files

or

tar -cvzf file_name.tar.gz folders

Here, c: means to create.

  • v: for Verbose shows processing during tar creation.
  • z: means gzip
  • f: stands for the file name of Tar

Create tar.gz from multiple files

tar -cvzf demo.tar.gz file1.txt file2.txt file3.txt

Output:

extract tar.gz file in Linux
create tar.gz from multiple files

Create tar.gz from the folder

The below command creates hello.tar.gz file from the folder hello.

tar -cvzf hello.tar.gz hello

Output:

How to extract tar.gz file in Linux
create tar.gz from the folder

Alternatively, you can create tar.gz from multiple folders.

tar -cvzf example.tar.gz folder2 hello

Output:

extract tar.gz file in Linux
create tar.gz from multiple folders

How to extract tar and tar.gz

You can extract the tar archive easily using the -x flag, as shown in the below syntax:

tar -xvzf /folder/your_tar_file.tar.gz

If you want to extract the tar file from your current directory, then you can simply do that by executing the following command:

tar -xvzf file_name.tar.gz

Let us understand by example:

Let’s say that you have a test.tar.gz file in your current working directory (/root/demo) and want to extract it in the same folder.

Example:

tar -xvf hello.tar.gz

Output:

extract tar.gz file in Linux
extract tar.gz file in Linux

Extract the tar archive to a specific path

You can extract the tar archive to the specific path with the use of the -C option, as shown in the below example:

Output:

How to extract tar.gz file in Linux
extract the tar archive

The above command, extract tar file in /tmp directory.

Extract a single file from a tar archive.

Syntax:

tar -xvf file_name.tar.gz [path/to/file]

The below command will extract file1.txt file from hello.tar.gz

tar -xvf hello.tar.gz hello/file1.txt

Output:

extract tar.gz file in Linux
extract a sigle file from a tar archive

Extract files using Wildcard

You can also extract files using a wildcard, For example, you can extract all the files with the .txt extension from hello.tar.gz by executing the below command:

tar xvf example.tar.gz --wildcards '*.txt'

Output:

extract tar.gz file in Linux
extract files from tar using a wildcard

Listing the contents of a tar.gz archive:

Occasionally, you want to just list the contents of a tar.gz archive to extract specific files or folders only.

You can list the contents of a tar.gz using the -t option, as shown in the following example:

Syntax:

tar -tf your_tar_filename.tar.gz

Example:

tar -tf example.tar.gz

Output:

How to extract tar.gz file in Linux
listing the contents of a tar.gz archive

Summary

Let’s summarize what we have covered in this article. We went through the basics of the tar tool and how to use it. Apart from showing the steps how to extract tar.gz file in linux, we also showcased the steps how to create tar archive and how to extract a single file from a tar archive.

Thank you very much for your time…

You may also like...

Leave a Reply

Your email address will not be published.