How to untar Files in Linux

How to untar Files in Linux

Tar is a widely used archive format, particularly on Linux systems. Tar archives are frequently used by distributions to provide package updates. Tar archives are also frequently encountered in online file sharing scenarios.

 

See our guide on untarring files under Linux.

 

Linux’s Tar

The tar utility is included with every Linux distribution for handling tar archives. We’ll use this tool to extract the contents of a tar archive.

Let’s use an example to clarify everything. Let’s start by making a tar archive that contains a number of files and directories. Here, I’ve made a directory called “Ants,” which is going to be converted to a tar archive.

 

$ tree Ants/

 

Let’s now create a tar archive from the directory. Here, tar will accomplish the task using a variety of compression algorithms. It is standard procedure for the output file name to be determined by the compression technique.

 

To generate a tar archive with gzip compression, run the following command.

 

$ tar -cvzf ants.tar.gz <source_file_directory>

 

To generate a tar archive using bzip2 compression, run the following command.

 

$ tar -cvjf ants.tar.bz2 <source_file_directory>

 

Use this command to produce a tar archive with XZ compression.

 

$ tar -cvJf ants.tar.xz <source_file_directory>

 

Tar file extraction

Enumerate the content of Tar.

 

To view a list of all the files and folders contained in the tar archive, use the tar command that follows.

 

$ tar -tvf <tar_archive>

 

Let’s quickly review the flags that we employed.

 

It instructs tar to make a list of the archive’s contents.

v: It instructs tar to print the action it took to the console.

f: It instructs tar on which file to execute the operation on.

Extract the complete file.

 

We may now begin extracting the tar archives from our current location. We can extract all of the tar archives with only one tar command, whereas you had to use many commands to build the various types.

 

Any legitimate tar archive can be extracted with the following tar command. Tar will replace the files outside the archive during extraction if there are files with similar names.

 

$ tar -xvf <tar_archive>

 

We have one new tar flag in front of us.

 

x: It instructs tar to begin extracting an archive.

Use the “-k” flag to tell tar not to overwrite any existing data. Tar is instructed not to change or overwrite any existing files or directories.

 

$ tar -xvkf <tar_archive>

 

Extract particular files.

 

In certain cases, you can extract a single file from a tar archive without needing to extract the entire archive. Because of its great versatility, the tar program allows you to extract just the necessary files.

 

The tar command structure would be as follows for this task. The file name in this case would be the name of the file you want. It needs to match the name of the file contained in the tar archive.

 

$ tar -xvf <tar_archive> <filename>

 

If you wish to extract a few of files in such a manner, use the following command structure.

 

$ tar -xvf <tar_archive> <filename_1> <filename_2>

 

Extract particular directories

 

This is just one more fantastic tar archive feature. You can manually instruct tar on which directory to extract, assuming the tar archive you are holding contains one or more directories.

 

The command structure resembles that of the preceding section.

 

$ tar -xvf <tar_archive> <directory>

 

Use the following command to extract multiple folders if desired.

 

$ tar -xvf <tar_archive> <directory_1> <directory_2>

 

The Fundamentals of the Linux tar -xvf Command

The fundamental Linux untar command is ‘tar -xvf’. Three options are combined into one: “x” for extract, “v” for verbose, and “f” for file. Each option accomplishes the following:

 

‘x’: This option instructs the tar program to extract a tar file’s contents.

‘v’: The verbose option allows the program to show the files’ extraction process in the terminal.

‘f’: You can enter the name of the tar file with this option.

In this instance, the contents of “archive.tar,” which include “file1.txt,” “file2.txt,” and “directory1,” are extracted using the “tar -xvf” command. Because of the ‘v’ option, the command shows these file names in the terminal while it extracts them.

 

Even if the “tar -xvf” command is an effective tool, there are some possible drawbacks. One frequent problem is that it will replace files that already exist without any notice. Therefore, it’s a good idea to use the ‘tar -tvf’ command to inspect a tar file’s contents before extracting it.

 

Advanced File Untarring Utilization

You can start experimenting with the more sophisticated aspects of the ‘tar -xvf’ command as you become more accustomed to using it. These include dealing with different forms of tar files, such as .tar.gz or .tar.bz2, and how to untar them.

 

Let’s become acquainted with a few of the command-line options or flags that can change the behavior of the tar command before delving into the more complex uses of the untar command. A table containing some of the most popular justifications for tar may be found here.

 

Example of Flag Description -xOpen an archive and use tar -xvf to extract files.tar -vList the files processed by the tar -xvf file in detail.tar -fUse archive file tar -xvf file.tar -zUse the gzip~tar -xzvf file to filter the archive.tar.gz -jFilter the archive with bzip2 tar -xjvf file.tar.bz2 -JUse the xz<tar -xJvf file to filter the archive.tar.xz -kDon’t overwrite already-existing files from the archive with the tar -kxvf file.tar -OExtract the files to a tar -Oxvf file in standard output.tar -mRestoring modification timings is not recommended.tar -mxvf file.tar -pMaintain file permissions by using tar -pxvf file.tar -CPrior to using tar -C /destination -xvf file.tar, change to the directory DIR.

After learning the fundamentals of the tar command line options, let’s explore the more complex applications of untarring files.

 

Handling.tar.gz Files:

 Tar files that have undergone gzip compression are known as.tar.gz files. You must use the ‘z’ option in order to untar these files:

 

tar -xzvf archive.tar.gz

 

Handling.tar.bz2 Files:

Tar files that have undergone bzip2 compression are known as.tar.bz2 files. You must use the ‘j’ option in order to untar these files:

 

tar -xjvf archive.tar.bz2

 

Examining Different Untar Techniques in Linux

Although the ‘tar’ command is the typical method for untarring files in Linux, you may also use the ‘gzip’ and ‘bzip2’ commands as alternate techniques. It’s important to know when to employ these alternatives because they can be helpful in specific situations.

 

Command Gzip

Using the Gzip compression method, files can be compressed and decompressed with the ‘gzip’ command. Two instructions are required in order to utilize “gzip” to decompress a.tar.gz file:

 

gunzip archive.tar.gz

 

Command Bzip2

The Bzip2 compression method can be used to compress and decompress files using the ‘bzip2’ command. Two instructions are required in order to utilize “bzip2” to decompress a.tar.bz2 file:

 

bunzip2 archive.tar.bz2

 

In this example, the ‘bunzip2’ command decompresses ‘archive.tar.bz2’ into ‘archive.tar’, and then the ‘tar -xvf’ command extracts the contents of ‘archive.tar’.

 

Solving Typical Linux Tar Command Problems

When untarring files under Linux, you could still run into problems even with the correct commands and settings. Let’s talk about some typical problems and solutions for them.

 

“tar: Error cannot be recovered”

This error usually appears in cases where the tar file is corrupt or doesn’t exist at all. The first thing you should do if you run across this error is make sure the tar file is intact. You can use the command “tar -tvf” to accomplish this:

 

tar -tvf archive.tar

 

Concluding remarks

Tar archives are rather easy to extract. The correct tar command is all that’s required. If you’re interested in completing the tasks using GUI and utilizing a file manager, then your file manager should have the ability to extract tar archives by default.

 

There are additional programs in Linux to extract different compressed archive formats.

 

Leave a Reply

Your email address will not be published. Required fields are marked *