What is Tar File?

The “tar” file contains a set of files/directories and metadata in Linux. These files are very useful as they take less disk storage and hold many files. These files can be compressed by having well-known extensions “tbz”, “.gz”, “.xz” and many others. Once created, they can be easily extracted using the “tar” command line tool.

This guide will detail the tar file’s objective, working, and usage. The outcomes of this guide are listed down:

Let’s start with the tar.

What is a Tar File?

The “tar (tape archive)” is the well-known command line tool used to create archive files. It extracts the archive file into the default and specific directory. The “tar” tool generates the “archives” to save the data storage that’s why it is called the “tape archive”.

The simple tar archive files combine the multiple files following the “.tar” extension. Whereas the compressed tar archive files have a “tar.gz” extension. Once the tar archive is created, it is opened using the “tar” command.

How to Manage Tar Files?

We need the tar command utility to manage and work with the tar files. We will first show the installation of the tar utility and then will show how it is used to manage the tar file.

Installing tar on Linux

The “tar” command line utility is available in almost all the commonly used distributions. Also, it is installed via the below commands:

$ sudo apt install tar         # Ubuntu, Debian and Mint
$ sudo pacman -S tar           # Arch Linux
$ sudo dnf install tar         # RHEL and CENTOS
$ sudo zypper install tar      # OpenSUSE 

Syntax of tar:

$ tar [Options].....[File]....

The above syntax holds the following components:

  • tar: Identifies the “tar” utility.
  • Options: Specifies the “tar” tool’s supported options.
  • File: Shows the tar archive files (compressed, decompressed).

The supported option of the “tar” command line tool can be easily accessed by typing its “help” command in the console (Ctrl+Alt+T):

$ tar --help

Scroll down the page to get brief information about the “tar” command.

How to Create a tar File?

The main objective of the “tar” utility is to create the tar archive file. This could be compressed by extensions “tar.gz”, “tar.xz”, “tar.bz2” and many others.

In this example, we created a simple tar archive file “Hello.tar” by using the following combination of “tar” command options:

$ tar -cvf Hello.tar Directory1

The above command contains the following options:

  • c: Creates the tar archive
  • v: Displays the verbose information of the archive on a terminal.
  • f: Tells the archive with the given filename

Hence the “Hello.tar” is created into the “home” directory.

How Does Create a Compressed tar Archive?

To create a compressed tar archive such as a “.gz” compressed archive file, use the “z” option of the “tar” command in the following way:

$ tar -zcvf Hello.tar.gz Directory1

How to Extract a “.tar” Archive File?

Execute the below-mentioned command to extract or open the “Hello.tar” file into the “Music” directory with the help of the “-x” and “-C” arguments of the “tar” tool:

$ tar -xf Hello.tar -C /home/itslinuxfoss/Music/

In the above command the “-C” flag denotes the absolute path of the target directory.

The “ls -lh” command confirms that the “Hello.tar” archive file has been extracted into the “Music” directory.

How to Extract a “.tar.gz” Archive File?

In this case, the same command is used to extract the “Hello.tar.gz” file into the “Documents” directory with a little bit of extension “.gz” difference as shown in the screenshot:

$ tar -xf Hello.tar.gz -C /home/itslinuxfoss/Documents/

It is verified that the “Hello.txt.gz” file content has been extracted into the “Documents” directory

Here, the content of the “Hello.tar” has been displayed in the terminal.

It’s all about the“tar” file creation extraction using the “tar” command line tool.

Conclusion

The “tar” file combines multiple files/directories and metadata in Linux. It is created via the “tar” command. This tool is audible in Linux to create or extract the “tar” archive file into the PWD and different directories. This guide has briefly illustrated the basics of tar files and the way to manage them.