Ubuntu

How to Create and Extract TAR.GZ Files on Ubuntu 22.04?

The zipped/compressed files are beneficial for reducing the size of various files and secure data transmission. The most used compression format in Linux is “tar.gz”, which stores multiple files in a single archive. To interact with the “tar.gz” files, the compression and decompression are the two keys The “tar.gz” files compression and decompression are handled through the Linux built-in “tar” command.

This post illustrates the complete process to create and extract “tar.gz” files on Ubuntu 22.04 with the following outcomes:

How to Create TAR.GZ Files on Ubuntu 22.04?

The “tar” command line tool handles a variety of compression formats such as “. xz”, “gz”, “.tbz”, “.bz2”, and so on. This section comprises the “tar” command used to create the “tar.gz” files on Ubuntu 22.04.

Syntax:
The generalized syntax to create a “tar.gz” file is written here:

$ tar czf [file_name.tar]

The syntax defines the following parameters:

  • tar: Represents the “tar” command line utility.
  • c: Creates the required tar file.
  • f: Specifies the archive file name.
  • z: Creates and extracts compressed files using “gzip”.
  • file_name.tar: Identifies the compressed tar archive.

There are four files available in the “Sample” directory as shown below:

$ ls -l

Example: Create a tar of Multiple Files
Create the “New.tar.gz” compressed tar archive of these four files using the above “tar” command syntax in this way:

$ tar czf New.tar.gz File1.txt File2.txt File3.txt File4.txt

The “New.tar.gz” has been successfully created in the “Sample” directory.

How to Extract TAR.GZ Files on Ubuntu 22.04?

The “tar” command is a handy tool that can extract the “tar.gz” files into the current working directory or the directory.

Syntax:
The basic syntax to extract/unzip the “tar.gz” files is typed here:

$ tar xzf [file_name.tar]

The “x” flag is used for the extraction of compressed files.

Example 1: Extract “tar.gz” Files in PWD
To extract the “tar.gz” file, simply utilize the above syntax with the created compressed “New.tar.gz” file in the terminal:

$ tar xzf New.tar.gz

All the compressed files have been extracted into the “Sample” directory.

Example 2: Extract “tar.gz” Files in Particular Directory
The “-C(–directory)” flag helps the users to extract the “tar.gz” files into the directory where he wants.

In this scenario, the “New.tar.gz” compressed archive located in the “Sample” is extracted into the “Downloads” directory using the “tar” command in this way:

$ tar xzf New.tar.gz -C /home/linuxhint/Downloads/

Let’s verify it using the “ls(list)” command followed by the “Downloads” directory:

$ ls -l Downloads

The output confirms that “New.tar.gz” has been extracted into the “Downloads” directory.

Example 3: Extract Specific File From “tar.gz”
Simple type the name of the specified file that you want to extract from “tar.gz” files with the “tar” utility in the following way:

$ tar xzvf New.tar.gz File2.txt

The “File2.txt” has been extracted from the “New.tar.gz” compressed file.

Conclusion

In Ubuntu 22.04, the pre-installed “tar” command utility supports the compression and decompression operation of “tar.gz” compressed files. This command utilizes its supported option to perform the desired task relying on their names. This post has listed a set of examples that demonstrate the creation and deletion of tar.gz files in Linux.

About the author

Adnan Shabbir