untar
How To

Untar Files in Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

While installing packages from the source, you are more likely to get the file in the .tar file.

Or you may have used the tar command to compress files in the past and now want to extract them.

But several types of tar files can be created with different utilities. For example, using gzip command, you can create .tar.gz file.

So in this tutorial, I will walk you through how you can untar files that were created with different utilities.

How to untar files in Ubuntu

In this tutorial, I will walk you through how you can untar the following file types:

  • Files having .tar extension
  • Files having .tar.gz or .tgz extension
  • Files with .tar.bz2 or .tbz extension
  • Files with .tar.xz extension
  • Files with .tar.lzma extension

Let's start with the first one.

How to extract .tar files in Ubuntu

You get the .tar extension when you use the tar command to create an archive from one or multiple files.

And to extract the tar file, all you need to do is use the -xvf option with the tar command and append the filename as shown:

tar -xvf file.tar

Now, let's breakdown the command:

  • -x is used to specify user wants to extract the file
  • -v gives the verbose output of what is being done by the command
  • -f is used to specify the file which needs to be extracted

For example, here, I have extracted a Fedora.tar file:

tar -xvf Fedora.tar

And as you can see, the file is extracted successfully and here I have used the tree command to show the file contents of the directory.

Similarly, if you want to extract the file somewhere else, you'd have to use the -C flag and provide the path to the tar command as shown:

tar -xvfC File.tar -C /path/to/extract
extract the tar file to different directory

How to untar .tar.gz or .tgz files in Ubuntu

You'd have .tar.gz or .tgz extension when you used the gzip command over the existing tar file or used the -z option while creating the tar file.

And to untar .tar.gz or .tgz files, all you need to do is add the -z flag to the previous command as shown:

tar -xzvf archive.tar.gz

Here, the -z flag indicates you want to work with the compressed file by gzip and when paired with the -x, it will extract the given file.

For example, here, I have untared the Fedora.tar.gz file:

tar -xzvf Fedora.tar.gz
untar tar.gz or tgz files in Ubuntu

How to untar .tar.bz2 or .tbz files in Ubuntu

If you have a file with .tar.bz2 extension, it clearly means the tar file was later compressed with the bzip2 command.

Whereas if you have a file with the .tbz extension, it indicates you used the -j flag while creating an archive with the tar command.

And in both cases, you can untar the file by using the -j flag with other options as shown:

tar -xjvf archive.tar.bz2

Here, the -j is used to indicate you want to work with the file compressed using the bzip2 command.

For example, here, I untared a Fedora.tar.bz2 file:

tar -xjvf Fedora.tar.bz2
untar tar.bz2 or tbz files in Ubuntu

How to untar .tar.xz files in Ubuntu

Having a tar file with the .tar.xz extension indicates that you have used the xz command over the tar file for compression.

And to untar a file having the .tar.xz extension, you'd have to use the -J flag with other options as shown:

tar -xJvf archive.tar.xz

Here, the -J flag is used to decompress a file compressed using the xz command.

For example, here, I have untared Fedora.tar.xz file:

tar -xJvf Fedora.tar.xz
untar tar.xz files in Ubuntu

How to untar .tar.lzma files in Ubuntu

If you have a tar file having the .tar.lzma extension, it means the user has used the lzma command to compress the tar file.

And to untar such files, you'd have to follow the 2-stage process as you can not untar such files directly.

First, you'd have to decompress the file, and to do so, you'd have to use the lzma command with the -d flag:

lzma -d archive.tar.lzma

For your reference, here, I have used Fedora.tar.lzma file:

lzma -d Fedora.tar.lzma

Once done, it should remove the .lzma extension:

decompress the files having  .tar.lz, .tlz or .tar.lzma extension

From here, you can use the same technique that I explained in the beginning as you are left with the file having the .tar extension:

tar -xvf file.tar
how to untar files in Ubuntu

That's it!

Want to learn how to create tar files?

If you want to learn how you can use the tar command to create different types of tar files, you can refer to the following guide:

How to Create and Extract Tarballs in Linux Command Line
Tar is one of the most common tool used for archiving files in Linux. Learn how to create a tarball and how to extract it in the beginner’s tutorial.

I hope you will find this helpful. And if you still have any queries, feel free to ask in the comments.



Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.