How to Extract or Untar tar.gz Files

untar tar.gz

If you use any open source software and use Linux, you’ll definitely need to untar tar.gz files at some point. (Tar is available on Linux.) If you want to extract or untar tar.gz files from your Linux-based VPS using command line options, there are several ways you can do this.

But first…

What are tar.gz files?

The Tar program lets you store files on magnetic tape. You can use Tar (available on Linux) to extract files from previously created archives, to store extra files, and even to update files that were previously stored.

Files that have a .tar.gz or a .tar.bz2 extension are compressed archive files. A file with just a .tar extension is uncompressed. (These are rare.)

The .tar section of the file extension stands for “tape archive”. Even though most of us have hard drives instead of magnetic tape, we still call them tar files.

What does it mean to untar tar.gz files?

To untar tar.gz files just means to extract the contents of the tar file (also known as a tarball).

Here’s the syntax to untar tar.gz files:

Syntax to extract or untar tar.gz files

tar [options] file.tar.gz
tar [options] file.tar.gz pattern
tar -xf file.tar.gz
tar -xvf file.tar.gz
tar -zxvf file.tar.gz
tar -zxvf file.tar.gz file1 file2 dir1 dir2

Want to untar tar.gz files? Here’s how you do it:

How to untar tar.gz files

To extract or untar tar.gz files from an archive, you need to enter:

$ tar -zxvf {file.tar.gz}


If your tar file is called example.tar.gz, enter the following at a shell prompt to extract files:

$ tar -zxvf example.tar.gz

To extract the file clownsdancingballet.doc from example.tar.gz, enter:

$ tar -zxvf example.tar.gz clownsdancingballet.doc

Where,

  1. -z : Work on gzip compression automatically when reading archives.
  2. -x : Extract tar.gz archive.
  3. -v : Produce verbose output (Display progress and extracted file list on screen).
  4. -f : Read the archive from the archive to the specified file. (In this case, read example.tar.gz.)
  5. -t : List the files in the archive.
  6. -r : Append files to the end of the tarball.
  7. –delete (GNU/Linux tar only) : Delete files from the tarball.

If you want to untar a tar file, you’ll need the -x and -f options.

TIP: Look before you untar tar.gz files

Sometimes, it’s fine to untar tar.gz files without even looking in them, but if you want to look before you untar, you can examine the contents of a tar file before you untar it by using the list option (-t).

tar -tf clownsdancingballet.tar.gz

You’ll notice that you don’t need to use the -z option to list the files. You only need to add the -z option when you’re extracting files from a .tar.gz file.

How to untar tar.gz archives in their entirety:

If you want to extract an entire archive, you’ll need to specify the archive file name with no individual file names as arguments. For example, to extract the entire example.tar.gz archive, enter:

tar -zxvf example.tar.gz

How to list files in the archive:

To look at a list of all the files in a tarball, enter:

$ tar -tvf example.tar.gz

How to create a tarball

In a GUI (also available on Linux), creating a tarball is a simple three-step process:

  1. Create a directory
  2. Place your files into the directory
  3. Right-click on the directory and select “Compress”

In a shell, the process is basically the same.

How to add to an existing tarball

You don’t need to untar tar.gz files every time you want to add a new file to the archive. If you’re using a Linux OS, you can open the tar archive just as you open any other directory. You can also add files whenever you wish.

How to choose where to untar tar.gz files to:

If you want to extract the files to a location other than the current directory, you’ll need to specify a target directory using the -c option (specified directory).

tar  -xvjf  clownsdancingballet.tar.gz   -C  ~/Desktop/Videos/

If you look in your Desktop/Videos directory, you’ll now see your videos of graceful clowns.

IMPORTANT: For this to work, the target directory must already exist on your device. Tar won’t create it out of thin air if it’s not already there. But if you need to create a directory and extract the files into it using just one command, here’s the command you need to enter:

mkdir  -p  ~/Desktop/Videos/Downloaded && tar xvjf   clownsdancingballet.tar.gz    -C ~/Desktop/Videos/Downloaded/

The -p (parents) option lets mkdir create and parent directories you need. This ensures that the target directory is created.

Conclusion

And that’s it! That’s how you untar tar.gz files. If you liked this guide, check out our other blog posts where we answer even more of your tech questions!