How To Extract Tar Files Into a Specific Directory

tar

In this article you will learn how to extract tar archives into a specific directory, as well as selecting and untarring only specific files and directories.

What Is the Tar Command?

tar is a very old command line utility from the seventies. tar stands for Tape ARchive, and it is used for creating and extracting archive files. Additionally, tar can be used to compress and decompress these archived files.

In other words, the tar program packs many files and folders into a single logical file for easy and fast transmission (e.g. FTP), optionally compressed. This essential tool is implementing various compression algorithms such as gzip, xz, and bzip2.

Tar comes with an enormous amount of options which are and can be confusing for even experienced Linux users.

However this article will cover only the subject of how to extract tar archives using special tar options to specify the target directory; in addition, we will mention other necessary tar arguments.

The Tar Command Syntax

tar <options> <archive> <file(s) or directories(s)>

Option(s) indicates which operation executes on the files (creation (-c), extraction(-x), compression (-z), etc.).

The archive indicates the file name and extension.

The file / directory name(s) indicates files and/or directories to be extracted or compressed.

Important tar options:

  • -C or --directory : target directory name for extracted files
  • -c : Creates an archive
  • -x : Extracts an archive
  • -f : Tar archive name
  • -t : displays or lists files in archived file
  • -v : Displays verbose information
  • -z : compresses the tar file using gzip compression program
  • -j : compresses the tar file using bzip2 compression program
  • -W : Verifies an archive file
  • -r : append files / directories to an already existing .tar file

Extracting Uncompressed Tar Archive

When you want to extract tar files to different directories you don’t have to change the directory using the cd command or by copying the tar archive to the desired directory then extract files.

We can achieve that by using the -C tar switch to specify where you extract the .tar file. Follow the example below:

tar -xvf myarchive.tar -C output/untar
Output
./
./Bbear_EP_03.ac3
./Bbear_EP_02.ac3
./Bbear_EP_01.ac3
./Bbear_EP_05.ac3
./Bbear_EP_04.ac3
./Bbear_EP_06.ac3
ls output/untar/
Output
Bbear_EP_01.ac3 Bbear_EP_03.ac3 Bbear_EP_05.ac3
Bbear_EP_02.ac3 Bbear_EP_04.ac3 Bbear_EP_06.ac3

We can also use the --directory tar command option just as so:

tar -xvf myarchive.tar --directory output/untar

As you might have noticed, the directory of the extracted file should be created beforehand. However we can easily create new directories and extract tar files using one command line.

Check the mentioned below command:

mkdir -p output/untar && tar -xvf myarchive.tar -C output/untar
Output
./
./Bbear_EP_03.ac3
./Bbear_EP_02.ac3
./Bbear_EP_01.ac3
./Bbear_EP_05.ac3
./Bbear_EP_04.ac3
./Bbear_EP_06.ac3
tree output/
Output
output/
└── untar
    ├── Bbear_EP_01.ac3
    ├── Bbear_EP_02.ac3
    ├── Bbear_EP_03.ac3
    ├── Bbear_EP_04.ac3
    ├── Bbear_EP_05.ac3
    └── Bbear_EP_06.ac3
1 directory, 6 files

Extracting Compressed Tar Archive

As we have mentioned before, the tar command can create and extract archive files using numerous compression algorithms such as xz, gzip, and bzip2.

In this section we will be acquainted with how we can extract different compressed tar archives.

Extracting Gz Files

To extract tar.gz or .tgz we will have to use the -z switch as mentioned below.

tar -zxvf myarchive.tar.gz -C output/uncompress

Extracting Bzip2 Compressed Files

Bzip2 is another file compression program used by the tar command line utility. To extract tar files compressed by this algorithm (e.g., tar.bz, tar.bz2 or .tbz2), we will simply add the -j tar switch just as so:

tar -jxvf myarchive.tar.bz2 -C output/uncompress

Extract Only Specific Files and Directories

The tar program also provides the possibility to extract specific files and directories. In this example we will extract from the .tar archive two files: Bbear_EP_04.ac3 and Bbear_EP_05.ac3 . Check the following tar command for better understanding.

tar -xvf myarchive.tar Bbear_EP_04.ac3 Bbear_EP_05.ac3 -C output/untar

Now let’s extract only a specific directory from the .tar archive.

tar -xvf mytests.tar bats/test_projects/ --directory extracted_tests/
Output
bats/test_projects/
bats/test_projects/test6.sh
bats/test_projects/test9.sh
bats/test_projects/test1.sh
bats/test_projects/test5.sh
bats/test_projects/test8.sh
bats/test_projects/test7.sh
bats/test_projects/test4.sh
bats/test_projects/test3.sh
bats/test_projects/test2.sh

As you will notice, this command will exclude any other directories and files within the archive.

Conclusion

In this article we’ve seen the different ways of extracting compressed and uncompressed tar files to a specific directory of our choosing.

Additionally, we have also learned about extracting only specific directories and files.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
bash if statement
Read More

Bash if..else Statement

The if..else statements are categorized under conditional statements in bash. They work similarly to the if..else statements in…