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:
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:
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:
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:
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:
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:
Let’s verify it using the “ls(list)” command followed by the “Downloads” directory:
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:
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.