How to Create Tar gz file

A tar file is an archive that stores many other files in form of a collection. The tar file stores additional information of files such as ownership, timestamp, and permissions. A tar file, comprised of many other files is also called a tarball. In Linux, the tar archive files are created using the tar command. While creating the tar archive files, the tar command compresses the file using a wide variety of algorithms. The gzip is the most general and frequently used algorithm for compressing tar files.

So, any file which ends with tar.gz or .tgz is a tar file compressed with the gzip algorithm.

The tar command pre-installed on various Linux-based distribution i.e. Ubuntu, Linux Mint, etc.

Syntax of tar command

The syntax of the tar command specifies how the tar command can be used to create tar gz files.

$ tar [options] [file name]

Various following options can be used with the tar command:

OptionDescription
-pThe –p option is used to store additional details of the file like permissions, ownership, and timestamp.
-cThe –c option creates a new tar file.
-zThe -z option sets the gzip compression method.
-fThe –f option creates a new file archive.
-vThe –v option displays the progress while creating the compressed file.
-xThe –x option extract the files from the tar.gz file.

Create tar.gz files in Ubuntu 20.04

The tar command comes pre-installed on Ubuntu 20.04, however, for any reason, if you have uninstalled it previously, then type the below-given command to install it:

$ sudo apt install tar

Create tar.gz from multiple files

Let’s create a tar file named ‘data.tar.gz’ from the files file1.txt, file2.txt, file3.json using the command:

$ tar -czf data.tar.gz file1.txt file2.txt file3.json

After the successful creation of the tar.gz file, the command does not display any output. Therefore, execute the ls command to see the newly created tar.gz file:

$ ls

The output of the ls command shows that the data.tar.gz file is created successfully.

If you want to create a new tar.gz file in another directory, then specify the full path of the directory in the tar command as follows:

 $ tar -czf /home/kamran/Downloads/mydataarchive.tar.gz file1.txt file2.txt file3.json

The above command created a mydataarchivetar.gz file in the Downloads directory of my Ubuntu 20.04.

We can create the tar.gz that comprises all the file which end with a specific extension.

For instance, let’s create a tar file from all the ‘.txt’ files:

$ tar -czf text.tar.gz *.txt

The text.tar.gz file is created successfully.

Create tar.gz from a single directory

Using the tar command, we can compress an entire directory and create a tar file. It also compresses the subdirectories of the stated directory. Let’s compress the Downloads directory (/home/Kamran/Downloads) and create a tar.gz file as follows:

$ tar -czf compressed.downloads.tar.gz /home/kamran/Downloads

The compressed.downloads.tar.gz file is created successfully.

Extract a tar.gz file

To extract an existing tar.gz archive file, we use the –x option with the tar command.

Following is the tar command syntax to extract an existing tar.gz file:

tar [options] [tar.gz archive file name]

Let’s extract a tar file by typing the command:

$ tar -xzf my.data.tar.gz

Wrapping up

The tar is a command-line utility that is used to create an archive file. It also uses compression algorithms to compress the data. The most popular compression algorithm is gzip and tar file compressed with the gzip algorithm has the extension .tar.gz. The tar.gz files can be created and extracted using the tar command.

Free Newsletter
Join our Newsletter and get the latest Linux news and tips
in your inbox for FREE.