How to Extract Tar Files to Specific or Different Directory in Linux

The tar utility is one of the utilities that you can use to create a backup on a Linux system. It includes many options that one can use to specify the task to achieve.

Extract Linux Tar Files Different or New Directory

One thing to understand is that you can extract tar files to a different or specific directory, not necessarily the current working directory. You can read more about tar backup utility with many different examples in the following article, before proceeding further with this article.

Again repeating that you must create a separate directory before unpacking files:

# mkdir -p /tmp/tar.bz2

Now we will be unpacking the documents.tbz2 files to /tmp/tar.bz2/ directory.

# tar -jvxf documents.tbz2 -C /tmp/tar.bz2/

Img 04: Extract tar.bz2 Files to Different Directory

Example 4: Extract Only Specific or Selected Files from Tar Archive

The tar utility also allows you to define the files that you want to only extract from a .tar file. In the next example, I will extract specific files out of a tar file to a specific directory as follows:

Summary

That is it with extracting tar files to a specific directory and also extracting specific files from a tar file. If you find this guide helpful or have more information or additional ideas, you can give me a feedback by posting a comment.

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

Great post. It would be helpful for Linux beginners to explain the switches used. What does -xf do? You explain the addition of the -v switch which is good. What’s the difference between -C and –directory? In Example 2, you throw in the -z switch and in Example 3, the -j switch without any explanation. Then in example 4, you talk about extracting single files, yet the command also includes an option to extract all files in a specific directory – etc/mysql/. Again, a good example for using different options, but it could use a bit of explanation.

Yes, we haven’t specified the meaning of each tar command option used in these examples, the reason because we’ve already requested users to read the article that says Mastering tar Command with this 18 Examples in Linux, before heading up further. No issue you can find all these options explanation below: