How to Extract Tar Files to in Linux

tar

In Linux, tar stands for Tape Archive. It is a command-line utility that is used for creating and manipulating archive files.

An archive file is a collection of files and directories bundled together into a single file. The tar command is particularly useful for creating backups, distributing files, and archiving data.

In Linux, the tar command is commonly used to archive and compress files and directories. To extract files from a tar archive, you can use the following command

tar -xvf your_archive.tar

Here’s a breakdown of the options used in the command:

  • -x: Extract files from an archive.
  • -v: Verbosely list the files processed.
  • -f: Specify the archive file.

Replace your_archive.tar with the actual name of your tar file. If the tar file is compressed (e.g., with gzip), you might need to use additional options. For example, if the file is named your_archive.tar.gz, you can use:

tar -xzvf your_archive.tar.gz

Here, the additional option -z is used to indicate that the archive is compressed with gzip.

If it’s compressed with bzip2, you would use -j:

tar -xjvf your_archive.tar.bz2

These are common options, and you might encounter variations based on the compression used. Always check the documentation or the help message of the tar command for more information:

man tar

This will display the manual page for the tar command, providing detailed information on its usage and options.

Extracting tar Files to a Specific Directory

When you extract files from a tar archive using the tar command in Linux, you can specify the destination directory where you want the extracted files to be placed.

To extract the contents of a tar file to a specific directory, you use the -C option followed by the target directory.

In the first example, I will extract the files in kb.tar to a directory /tmp/kb. Always make sure that the directory into which you want to extract tar file exists.

Let me start by creating the /tmp/kb directory using the command below

mkdir /tmp/kb

You can include the -p option to the above command so that the command does not complain.

To extract the files in kb.tar to /tmp/kb I will run the command bellow:

tar -xvf kb.tar -C /tmp/kb

Extract .tar.gz or .tgz Files to Different Directory

To extract a .tar.gz or .tgz file to a different directory in Linux, you can use the tar command with the -C option to specify the destination directory. Here’s the basic syntax:

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 docs.tgz file to separate /tmp/tgz/ directory.

tar -zvxf docs.tgz -C /tmp/tgz/

Extract tar.bz2, .tar.bz, .tbz or .tbz2 Files to Different Directory

To extract .tar.bz2, .tar.bz, .tbz, or .tbz2 files to a different directory in Linux, you can use the tar command with the -C option to specify the destination directory.

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

mkdir -p /tmp/tar.bz2

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

tar -jvxf docs.tbz2 -C /tmp/tar.bz2/

Extract Only Specific or Selected Files from Tar Archive

To extract only specific or selected files from a tar archive in Linux, you can use the --extract or -x option along with the names of the files you want to extract.

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/

That is it with extracting tar files to a specific directory and also extracting specific files from a tar file.