Linux Commands

How to Untar Files in Linux

Tar is quite a popular archive format, especially on Linux. In many cases, distros use tar archives to deliver package updates. Tar archives are also common to find when it comes to sharing files online.

Check out how to untar files in Linux.

Tar in Linux

For managing tar archives, all the Linux distros come with the tar tool. It’s this tool that we’ll use for extracting the contents of a tar archive.

Let’s demonstrate everything with an example. First, let’s create a tar archive with several files and directories. Here, I’ve created a directory with the name “Ants” that will be transformed into a tar archive.

$ tree Ants/

Now, let’s make a tar archive out of the directory. Here, tar will use various compression algorithms to do the job. It’s a common practice that the compression algorithm dictates the output file name.

To create a tar archive using gzip compression, use the following command.

$ tar -cvzf ants.tar.gz <source_file_directory>

To create a tar archive using bzip2 compression, use the following command.

$ tar -cvjf ants.tar.bz2 <source_file_directory>

To create a tar archive using XZ compression, use the following command.

$ tar -cvJf ants.tar.xz <source_file_directory>

Extracting tar files

List tar content

The following tar command will list all the files and directories included in the tar archive.

$ tar -tvf <tar_archive>

Let’s have a quick breakdown of the flags we used.

  • t: It tells tar to list the contents of the archive.
  • v: It tells tar to print its action on console.
  • f: It tells tar which file to perform the action on.

Extract entire file

Now, we’re ready to extract the tar archives we’ve got at hand. While you needed to use different commands to create different types of tar archives, we can use only a single tar command to extract all of them.

The following tar command will extract any valid tar archive. If files with similar filenames exist, upon extraction, tar will overwrite the files outside the archive.

$ tar -xvf <tar_archive>

Here, we’re facing one new tar flag.

  • x: It tells tar to extract an archive.

If you don’t want tar to overwrite existing data, add the “-k” flag. It tells tar not to overwrite/replace any existing file or directory.

$ tar -xvkf <tar_archive>

Extract specific files

There are some situations where you don’t need the entire tar archive extracted only to grab a single file. The tar tool offers such flexibility that you can extract only the select few files you need.

For this task, the tar command structure would look like this. Here, the file name would be the file name of your desired file. It must match with the file name that’s inside the tar archive.

$ tar -xvf <tar_archive> <filename>

If you want to extract a couple of files in such a manner, use the following command structure.

$ tar -xvf <tar_archive> <filename_1> <filename_2>

Extract specific directories

This is yet another awesome feature of the tar archive. Assuming the tar archive at your hand contains a directory or more, you can manually tell tar which directory to extract.

The command structure is similar to the section above.

$ tar -xvf <tar_archive> <directory>

If you want to extract multiple directories, then run the following command.

$ tar -xvf <tar_archive> <directory_1> <directory_2>

Final thoughts

Extracting tar archives is quite a simple task. All you need to know is the right tar command. If you’re interested in performing the actions with GUI and using a file manager, then your file manager should have the ability to extract tar archives by default.

In Linux, there are more tools to extract various formats of compressed archives. Check out how to extract compressed archives in Linux.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.