Puristus

How to Untar Files in Linux

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

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

$ tar -cvjf ants.tar.bz2

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

$ tar -cvJf ants.tar.xz

Extracting tar files

List tar content

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

$ tar -tvf

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

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

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

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

$ tar -xvf

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

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

$ tar -xvf

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!

Kuinka käyttää GameConqueror-huijausmoottoria Linuxissa
Artikkelissa on opas GameConqueror-huijausmoottorin käytöstä Linuxissa. Monet Windows-pelejä pelaavat käyttäjät käyttävät "Cheat Engine" -sovellusta m...
Parhaat pelikonsoliemulaattorit Linuxille
Tässä artikkelissa luetellaan suositut pelikonsolin emulointiohjelmistot, jotka ovat saatavana Linuxille. Emulointi on ohjelmistojen yhteensopivuusker...
Best Linux Distros for Gaming in 2021
The Linux operating system has come a long way from its original, simple, server-based look. This OS has immensely improved in recent years and has no...