What is tar Command in Linux | Examples

When it comes to compressing/archiving files in Linux, the tar command prompts out as a well-known solution. The tar command helps in creating and extracting one or more files. By archiving files, it becomes really easy to move files between directories or across systems. The tar command supports various formats and all these formats along with the examples will be explained in depth in this post.

This guide will demonstrate the brief working and utilization of the Tar Command in Linux, which includes creating, listing, modifying, and extracting archive files in Linux. The post’s content is as follows: 

Let’s start the guide on the tar command in Linux.

How tar Command Works in Linux?

When you are planning to learn about any command, then the first thing you should do is understand the syntax and how it can be used.

Syntax

The basic syntax of the tar command is shown below:

$ tar [Options] [File]

Note: The list of options supported by the tar command can also be obtained using the following command.

$ tar --help

As we can see in the above image, it provides some examples of how you can implement this command and the information associated with its flags. Let’s see how the tar command can be used in Linux.

How to Use tar Command to Create Archive Files?

If you are dealing with single or multiple files and want to create their archive, then you can do that using the tar command.

Example 1: Create an Archive of Single File

For instance, the command written below will create a tar file named ‘archive.tar’ which contains the file named ‘testfile’ :

tar -cf archive.tar testfile

Whereas ‘-c’ is a flag to create a file and ‘f’ shows that you want to create the archive file. The above command will create the archive file with the name ‘testfile’ and save it inside ‘archive.tar’. The above command will not only create the tar file, but the original file can also be found in the same directory.

Now, we can see the content of this file and verify if it exists in the ‘archive.tar’ file by typing:

$ tar -tf archive.tara

Example 2: Create an Archive of Multiple Files

Similarly, we can create an archive file for multiple files. For instance, the command provided below creates an archive file of “testfile_1”, “testfile_2”, and “testfile_3”:

$ tar -cf archive.tar testfile_1 testfile_2 testfile_3

And then we can see the content of the ‘archive.tar’ similarly by typing the same command discussed above:

$ tar -tf archive.tar

This is how we can create the tar file, and in the next section, we will be discussing how you can extract the tar file.

How to Use tar Command to Extract Archive Files?

You can either extract the file in the same directory where the archive file resides or extract it in a different directory as well, which will be discussed in detail in the below section.

Example 1: Extracting Files in the Same Directory

Let’s suppose that you want to extract the content of the same archive.tar file that was created in the previous sections. You can see in the below image that there is one archive file, but its content is hidden:

So, to extract the content of this file, you can use the ‘x’ flag using the following command in the terminal:

$ tar -xf archive.tar

You can also see these files directly by opening the directory as below:

Example 2: Extracting File in the Different Directory

You can see in the above image that the extracted files are currently in the same directory so to extract the file in the ‘Documents’ directory you need to type the complete path of the target directory as follows:

$tar -xf archive.tar /home/itslinux/Documents

Bonus Tip: Different Formats Supported by the tar Command

In Linux, the tar command supports several different archive formats, including the following:

.tar.gz or .tgz: This format uses gzip compression to compress the contents of the archive, resulting in smaller file size.

.tar.bz2 or .tbz: This format uses bzip2 compression to compress the archive’s contents, resulting in a smaller file size than the .tar.gz format.

.tar.xz or .txz: This format uses xz compression to compress the archive’s contents, resulting in a smaller file size than the .tar.bz2 format.

That’s all about the tar command in Linux.

Conclusion

In Linux, the tar command is used to manage the archive files, i.e., creating and extracting the archive files of different formats. This post has taught you about the basic working of the tar command by explaining the syntax and various options supported by it. Moreover, you have also learned to use the tar command to create and extract archive files in Linux.