How To Extract .tar.gz Files using Linux Command Line

Posted on January 5th, 2017

In this tutorial we can learn how to extract tar.gz files using Linux Command line tools.

A .tar.gz file is nothing, but an archive. It is a file that acts as a container for other files. The tar program provides the ability to create tar archives, as well as various other kinds of manipulation. For example, you can use Tar on previously created archives to extract files, to store additional files, or to update, or list files which were already stored. An archive can contain many files, folders, and subfolders, usually in compressed form using gzip or bzip2 program on Unix operating systems. Initially, tar archives were used to store files conveniently on magnetic tape. The name “Tar” comes from this use; it stands for tape archiver. You need to use the tar command which can create and manipulate archive files in .tar.gz under Unix like operating systems. It is very useful for archiving multiple files together into a single archive file. It allows us to restore files individually. Tar can direct its output to available devices, files, or other programs (using pipes), it can even access remote devices or files (as archives). tar.gz file is actually the product of two different things. Tar basically just packages a group of files into a single file bundle, but doesn’t offer compression on it’s own. To compress tar you’ll want to add the highly effective gzip compression. In this documentation, we can discuss about how to extract the tar.gz files from the command line. For this, open a command-line terminal and then type the following commands to open and extract a .tar.gz file.

 

Extracting .tar.gz files

1) If your tar file is compressed using a gzip compressor, use this command to uncompress it.

$ tar xvzf file.tar.gz

Where,

x: This option tells tar to extract the files.

v: The “v” stands for “verbose.” This option will list all of the files one by one in the archive.

z: The z option is very important and tells the tar command to uncompress the file (gzip).

f: This options tells tar that you are going to give it a file name to work with.

A tarball is a group or archive of files that are bundled together using the tar command and have the .tar file extension.

Eg:

How To Extract .tar.gz Files using Linux Command Line

 

2)  To uncompress tar.gz file into a different directory, use the command below:

$ tar xvzf file.tar.gz -C /path/to/somedirectory

Where the -C argument is used to specify the path to place the file. By defaults files will be extracted into the current directory. To change the directory, we use -C option.

Eg:

How To Extract .tar.gz Files using Linux Command Line

 

 

3) To extract test.doc file from the file.tar.gz tarball, use the following command:

$ tar -xvzf file.tar.gz test.doc

Eg:

How To Extract .tar.gz Files using Linux Command Line

 

4) To view a detailed table of content by listing all files in archive, you can use the following command:

$ tar -tvf file.tar.gz

Eg:

How To Extract .tar.gz Files using Linux Command Line

 

5) To extract the .gz file without tar, you can use the following command:

$ gunzip file.gz

Eg:

How To Extract .tar.gz Files using Linux Command Line

 

Extracting .tar.bz2 files

This is just about the same as the gzip decompression. The major difference is that the z option has been replaced by the j option.

If your tar file is compressed using a bZip2 compressor, use the following command to extract it.

$ tar xvjf file.tar.bz2

Where ‘j’ will decompress a bzip2 file.

Eg:

How To Extract .tar.gz Files using Linux Command Line

 

If you need any further assistance please contact our support department.

 

 

22 Responses to “How To Extract .tar.gz Files using Linux Command Line”

  1. Faizan says:

    Nice explanation. Very helpful. Thanks a lot.

  2. naziya says:

    i have total 125 tar.gz files to unzip can you suggest a command to unzip all 125 tar.gz in a single stretch

    • Michael Lavrik says:

      use *.tar.gz for the file name.

    • Adarsh Sojitra says:

      If you want to extract all the tarballs at once, in one location, use wildcard characters in command. For example, if you want to unzip all the tarballs at once located in the current directory, execute sudo tar -xzvf *.tar.gz". Let us know if you have more questions.

  3. Igboanugwo Collins says:

    So helpful, thanks a lot

  4. gc337 says:

    i have files with multiple extension. how can i extract this kind of file 21.zip.gz.gz.gz.gz.

  5. ringo says:

    i keep getting this “not in gzip format” how can i fix that??

  6. Austin says:

    Im using Chromebook how would i do this?

  7. miki says:

    Ive created a abc.tar.gz file and when I try to decompress it, I use the command tar -zxvf abc.tar.gz. All files are extracted. And when I just use tar -xvf abc.tar.gz, the output is the same. So does -z really matter when extracting those .tar.gz files?

  8. abdul majid says:

    hi, I used this command “tar xvf informix_setup.tar -C ./working/new” to extract a .tar file to the ‘new’ directory but it is not extracting anything in the given path.

  9. Orhan says:

    Thank you very much. very simple and well explained and very helpful.

  10. Sam says:

    Hello, I want to untar a tar.gz file.
    I have run the command as: tar -zxvf (file name).tar.gz
    But it shows the below error:

    tar (child): HEAL_2021_05.tar.gz: Cannot open: No such file or directory
    tar (child): Error is not recoverable: exiting now
    tar: Child returned status 2
    tar: Error is not recoverable: exiting now

    Could you please provide a solution?

    • As per the error (Cannot open: No such file or directory), it seems your present working directory (pwd) is different from the directory in which tar file actually exists. Kindly cross-check, or please contact our support via live chat or ticket system, so we can better assist you directly.

  11. Anterus says:

    Please what’s the command to extract a file into the log file

  12. starkiller says:

    i keep getting command not found, how do i fix this?

  13. Jozo says:

    MotorsportManager-v1.5.16749.tar.xz is file name, not gz on the end. New to linux, don’t get it all.

  14. Tayvon says:

    hey i have a file in xz format not gz what do i do with this?

Leave a Reply