Linux Commands

What is a Tar File

Tar is a powerful archiver that is frequently used for collecting files and archiving them. It was created to produce archives for data storage on tapes, thus the name “Tape Archive”. It was initially included in UNIX version 7 in 1979, and it is currently accessible on a variety of systems.

Before we go into the situation’s specifics, let’s define Archive files so that no Linux newbies are left out. Archived file is a combination of multiple files with metadata information. By combining multiple files and their information into a single file, you may improve the storage and mobility of your data. The basic purpose of Tar is to combine the data but you can also compress the data using other utilities. These compressed files are referred to as Archive files, and they assist users to reduce file size and simplify data management. The tar is one of the must-have utilities for managing various files in Linux.

How Tar file works in Linux

To understand the basic working and syntax of a tar utility, you need to type the below-given command in the terminal:

$ tar --help

You will see all the details after entering this command in the terminal that we will also discuss in this article. So after looking at the image you can find the basic syntax of a tar file:

$ tar [OPTION…] [FILE…]

Here OPTION represents your operation choice as to how you want to create a tar file. So before going into the details of creating a tar file you should first confirm whether the tar utility is installed in your system or not otherwise you won’t be able to create a tar file. You can verify that by typing.

$ tar --version

As you can see that the tar utility is already installed, and its current version is 1.30, but if by any chance this utility is not installed in your system, then you can do that by entering the command:

$ sudo apt install tar

Some useful options that are used with the tar command are listed below:

Options Description
c To make (create) a new archive file
f/d signify file/directory
x To extract the content of the archive file
v To provide you the additional details of the archive file (Verbose)
t To list the files
z To filter .gz file

How to make a tar file

To give you a basic understanding of creating a tar file, we have created a folder with three text files that you can see in the documents directory, as shown below.

The next step is to make a tar file that is possible by typing.

$ tar -czvf my_folder.tar.gz *

Or you can also type:

$ tar -cvjf my_folder.tar.bz2 *

The above commands are used not only to make a tar file but also compress them in the same folder where the files are present, and you can see that these two commands have created a tar file with .bz2 and .gz format. Also, the asterisk sign (*) ensures that the tar file should include all the files in the current directory in a sequence. Later you can verify the newly created compressed tar file by opening a folder as shown below.

How to extract a tar file

So, if you have downloaded a .tar file from the internet or someone else sends you then you can extract its content by typing.

$ tar -xvf my_folder.tar.gz

Or:

$ tar -xvf my_folder.tar.bz2

How to view the content of a tar file

If you have a tar file but you only want to see its content and don’t want to extract it then you can do that by typing.

$ tar -tf my_folder.tar.gz

The above command will give you the basic details about the files that are present inside the tar. So if you are interested in getting more details about the tar file then you can do that by typing.

$ tar -tvf my_folder.tar.gz

Conclusion

Tar is a powerful Linux application that not only lets you create tar files but can also extract them. Moreover, you can also inspect the contents of various tar archive files without having to extract them. In this tutorial, we have learned how to use different options and commands in the terminal to inspect the contents of tar files and compress the tar files.

About the author

Taimoor Mohsin