A tar.gz file is a compressed archive format. It is widely used in Linux and Unix-based systems. It combines Gzip compression and TAR file formats. These are two distinct types of file formats. The Gzip file format is used to compress the archive and reduce its size for effective storage and transfer. The TAR file format bundles many files and directories into a single archive file. If you need a version control tool to help you, then learn to stash an untracked file in Git.

A benefit of tar.gz files is their ability to compress big files and directories into one archive. This makes it easier to transport them over networks or store them on external devices. Also, the compression saves disk space, especially with large data sets.

Using the Linux command line, you can extract a tar.gz file to a different directory by using the -C option and the directory path. the following steps:

  1. Activate the terminal window.
  2. Use the cd command to move to the directory containing the tar.gz file.
  3. Use the tar command with the arguments -xvf followed by the filename and the -C option followed by the directory path where you want to extract the contents if you want to extract the file to a separate directory.

The command would be as follows, for instance, if the file name is example.tar.gz and you wish to extract the files to the directory /home/user/documents/:

tar -xvf example.tar.gz -C /home/user/documents/

The tar.gz file’s contents will be extracted and placed in the directory you specified once the command has been executed.

How to Extract a tar.gz File

The tar command is typically pre-installed on Linux distributions by default.

tar xvzf file.tar.gz

X: When selected, tar is instructed to extract the files.

v: Verbose output displays every file that is being extracted.

Z: Instructs tar to decompress the archive using gzip.

F: The command’s final flag must be this one. The name and path of the compressed file are provided to tar.

File tar.gz Extraction to a Different Directory

tar xvzf file.tar.gz -C /tmp/archive

The current working directory is not the only directory that can be specified using the -C option.

List of Files to view within an Archive

Because tar files collect numerous files and check to see if a certain file is present, you may occasionally need to inspect their contents.

Use the following syntax to view a thorough table of contents for the archive data.tar.gz:

tar tf data.tar.gz

view list of files in tar.gz

Where:

t: Used to list the contents of the tar file
f: Instructing the tool to utilize the file specified in the following argument

You can also get detailed standard output by using the v (verbose) option.

tar tvf data.tar.gz

check list of files in tar.gz

Wrapping up on Unzip/Extract tar.gz Files Command in Linux

You now understand how to use Linux to untar a tar.gz file. How to unzip files in Linux might be something else that interests you. Consult the tar command’s manual page for more information.

Hope this article is useful for the queries you had regarding extracting the tar.gz files in Linux and anything related to that. Tried to explain in detail and by the steps of an easy way to make the confusion clear to everyone who needs to understand it.

Frequently Asked Questions (FAQ)

1. What is a .tar.gz file?

.tar.gz file merges numerous files or directories into a single .tar file, then compresses it using gzip to reduce its size, resulting in a .tar.gz extension. A .tar.gz file is a compressed archive in Linux.

2. Can I Extract a .tar.gz File on any Linux Distribution?

Yes, the tar command is versatile across Linux allocations, so the procedure should function on most systems.

3. How do you Unzip tar.gz Files in Linux?

To extract the contents of a tar.gz file, use the command below:

tar -xvzf filename.tar.gz  

-x: Extracts the files.

-v: Shows the extraction procedure (optional)

-z: Controls gzip compression.

-f: Indicates the file to retrieve.