To totally unlock this section you need to Log-in
Login
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.
One thing to understand is that you can extract tar files to a different or specific directory, not necessarily the current working directory.
The general syntax of tar utility for extracting files:
# tar -xf file_name.tar -C /target/directory # tar -xf file_name.tar.gz --directory /target/directory
Note: In the above first syntax, the -C option is used to specify a different directory other than the current working directory.
Let us now look at some examples below.
Example 1: Extracting tar Files to a Specific Directory
In the first example, we will extract the files in articles.tar to a directory /tmp/my_article. Always make sure that the directory into which you want to extract tar file exists.
Let me start by creating the /tmp/my_article directory using the command below:
# mkdir /tmp/my_article
You can include the -p option to the above command so that the command does not complain.
To extract the files in articles.tar to /tmp/my_article, we will run the command bellow:
# tar -xvf articles.tar -C /tmp/my_article/
In the above example we used the -v option to monitor the progress of the tar extraction.
Let me also use the --directory option instead of -c for the example above. It works just in the same way.
# tar -xvf articles.tar -directory /tmp/my_articles/
Example 2: Extract .tar.gz or .tgz Files to Different Directory
First make sure that you create the specific directory that you want to extract into by using:
# mkdir -p /tmp/tgz
Now we will extract the contents of documents.tgz file to separate /tmp/tgz/ directory.
# tar -zvxf documents.tgz -C /tmp/tgz/
Example 3: Extract tar.bz2, .tar.bz, .tbz or .tbz2 Files to Different Directory
File extension .tar.bz2 is and archived file which is created with tar and bzip together. GNU ‘tar‘ is very useful for archiving multiple files together into a single archive file. It allows us to restore files individually.
# tar -xvjf filename.tar.bz2
anaconda.log boot.log kdm.log mysqld.log scrollkeeper.log yum.log
Creating .tar.bz2 File
To create .tar.bz2 ( tar + bzip ) file using command line use following syntax. We can also use wild card characters to archive multiple files.
# tar -cjf archive.tar.bz2 file1.txt *.log
Listing Files in .tar.bz2 File
To list all files archived under a .tar.bz2 file without extracting them use following command.
# tar -tvzf archive.tar.bz2
-rw-r--r-- root/root 119783 2014-12-31 06:22:22 file1.txt -rw------- root/root 863287 2014-08-14 17:35:58 anaconda.log -rw------- root/root 0 2014-12-29 02:17:27 boot.log -rw-r--r-- root/root 0 2014-12-29 02:17:26 kdm.log -rw-r--r-- root/root 25643 2014-12-29 01:12:35 Xorg.0.log -rw-r--r-- root/root 11380 2014-11-24 17:35:39 yum.log
Unpacking tar.bz2
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/
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:
# mkdir /backup/tar_extracts # tar -xvf etc.tar etc/issue etc/fuse.conf etc/mysql/ -C /backup/tar_extracts/
Summary
That is it with extracting tar files to a specific directory and also extracting specific files from a tar file.