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.
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.
To create a tar archive using bzip2 compression, use the following command.
To create a tar archive using XZ compression, use the following command.
Extracting tar files
List tar content
The following tar command will list all the files and directories included in the 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.
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.
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.
If you want to extract a couple of files in such a manner, use the following command structure.
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.
If you want to extract multiple directories, then run the following command.
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!