How to Extract or Unzip .tar.gz Files in Linux

Learning how to extract or unzip .tar.gz files in Linux is essential for managing software packages and backups effectively. Creating tar archives involves converting a collection of files into an archive. These archives are commonly utilized for backup or software distribution purposes. Creating tar archives is possible in Linux and Unix systems by utilizing the tar command. It is compatible with a wide variety of compression programs, including gzip, bzip2, lzip, lzma, lzop, xz, and compress. Tar was initially developed to create archives for storing files on magnetic tape, hence its name “Tape ARchive.”

PRODUCTS THAT MIGHT INTEREST YOU:

Benefit from the best server plans and related services, competitive prices, coupled with personalized attention to each client. Supported by top-notch technical assistance that remains consistently accessible to address all your inquiries.

The Gzip algorithm is widely used for compressing tar files. Typically, a tar archive compressed with gzip should have a file name that ends with either .tar.gz or .tgz.

Simply put, a file with the extension .tar.gz is a compressed .tar archive.

In addition to creating new archives, the tar command has a range of other functionalities. These include extracting tar archives, displaying a list of files within an archive, and adding more files to an existing archive.

Open-source packages are commonly distributed in .tar.gz and .zip formats. If you frequently work with open-source software, it’s likely that you come across .tar.gz files quite often.

Prerequisites

  • A Linux system (this tutorial uses Ubuntu 22.04).
  • Access to a terminal.

Listing the contents of a .tar.gz file in Linux

If you do not currently possess a .tar.gz file, you can generate one by following these steps:

tar -czf [archive name] [file(s)/location(s)]

Here’s how the options -cvf function:

c – creates a new archive.

z – compresses the file.

f – specifies the file name.

As an example, the Home directory has three files (File1, File2, File3) that have been confirmed using the ls command:

ls

Create a .tar.gz archive named example1 by compressing the three files using the tar command.

tar -czf example1.tar.gz File1.deb File2.deb File3.deb

There is no output from the command. For confirmation, execute the ls command once more.

ls

The output displays the creation of a fresh archive called example1.tar.gz.

To display the contents of a .tar.gz file, use the following command:

tar -ztvf [archive name]

As an example, you can use the following command to list the contents of example1.tar.gz:

tar -ztvf example1.tar.gz

How to extract .tar.gz files in Linux using the terminal

Extracting .tar.gz files using the terminal is incredibly convenient as it allows users to easily modify the commands by utilizing various options. Here are three tools you can use to unzip .tar.gz archives in Linux.

How to extract .tar.gz files in Linux using the tar command

To extract the .tar.gz file, utilize the tar command along with the specified arguments:

tar –xvzf [archive name]

There are four options that you need to use with the basic command tar:

  • Use the “x” command to extract the files from the zipped file.
  • v – displays the files that are being extracted.
  • Use the “z” option to decompress the files in tar.
  • Specify the filename to tar using the -f option.

Extract the .tar.gz file in the current directory.

Extract the example1 archive in the current directory using the command provided:

tar -xvzf example1.tar.gz

The command will extract File1, File2, and File3.

Extracting files to a specific directory

To extract the files to a designated directory, such as Documents, execute the following command:

tar -xvzf example1.tar.gz -C ./Documents

Extracting only specific files

To extract a particular file from the archive, simply include the file name in the command. For instance, you can extract File1 from example1.tar.gz using:

tar -xvzf example1.tar.gz File1.deb

Extracting files with a specific extension or name

To extract all files with a specific extension or name, you can utilize the –wildcards option. For example, you can use the following command to extract all files with the .deb extension:

tar -xvzf example1.tar.gz --wildcards '*.deb'

Extracting files using gzip

Gzip is a handy tool that allows you to compress files or merge multiple files into one compressed file. It’s a command-line utility that can significantly reduce file sizes. Additionally, when using the -d option, gzip has the capability to decompress .tar.gz files. Here is the syntax:

gzip -d [archive name]

To unzip example.tar.gz, you can use the following command:

gzip -d example.tar.gz

No output is displayed by the command. Execute the ls command to confirm the result:

ls

The output indicates that the example1.tar.gzip file was successfully extracted to example1.tar. To extract files from the .tar archive, follow these steps:

tar -xf example1.tar

There is no output from the command. Confirm the modifications using ls:

ls

Extract files using the gunzip command

One alternative method for unzipping a .tar.gzip file is by utilizing gunzip. The gunzip tool is a command that serves as the opposite of gzip and performs the same function as gzip -d. Here is the syntax:

gunzip [archive name]

To extract files, you can utilize gunzip on the example1.tar.gz file.

gunzip example1.tar.gz

There is no output from the command. Please run the ls command to confirm:

ls

The command will extract File1.deb, File2.deb, and File3.deb. It also converts example1.tar.gz to example.tar.

Extracting files from a .tar.gz using a graphical user interface

An easy way to extract files from a .tar.gz archive is through a Graphical User Interface (GUI). For newcomers, a GUI is a better choice compared to a command-line tool.

Extract files to the current directory

First, apply a graphical user interface to extract the files in the present directory. Here are the steps you need to follow:

Find the .tar.gz file that needs to be unzipped. This example utilizes example1.tar.gz in the Home directory.

Step 2: To perform the desired action, simply right-click on the file.

3. Choose the option to extract the files in this location.

The command will extract the files to a new directory called example1, which will be located in the current directory.

Extracting files to a specific directory

To extract the files from a .tar.gz archive and move them to a designated directory, you can follow these instructions:

First, locate the .tar.gz file you wish to unzip. In this particular scenario, the file is example1.tar.gz.

Step 2: To perform the desired action, simply right-click on the file.

3. Select the option to Extract to.

4. Select the directory where you want to extract your files. In this particular case, it’s Documents.

5. After selecting the directory, simply click the Select button located in the top right corner.

The files are visible in the directory you chose.

Troubleshooting Tips: Common Issues and Solutions When Extracting .tar.gz Files

  1. Permission Denied:

Issue: You encounter a “Permission denied” error when attempting to extract files.

Solution: Use sudo to run the extraction command with superuser privileges. For example:

sudo tar -xvzf example1.tar.gz
  1. File Not Found:

Issue: You receive a “File not found” error.

Solution: Ensure you are in the correct directory or provide the full path to the .tar.gz file. Check the file name for typos. Use the ls command to list files in the directory and verify the file’s existence.

  1. Checksum Errors:

Issue: You see a checksum error indicating the file may be corrupted.

Solution: Re-download the .tar.gz file from the source to ensure it is not corrupted. Verify the integrity of the downloaded file using checksums (e.g., SHA256).

  1. Extraction Fails Partway:

Issue: The extraction process starts but fails before completion.

Solution: Check available disk space using the df -h command. Ensure you have enough free space to extract all files. If space is sufficient, the archive might be corrupted, so try re-downloading it.

  1. Unexpected EOF (End of File):

Issue: You encounter an “Unexpected EOF” error.

Solution: This often indicates a corrupted archive. Re-download the file. If you are creating the archive, ensure it completes successfully without errors.

Real-World Use Cases

Examples of When and Why You Would Extract .tar.gz Files in a Practical Scenario

  1. Software Installation:

Many open-source software packages are distributed as .tar.gz files. Developers and system administrators download these archives, extract them, and follow the included installation instructions to install software on their systems.

  1. Backup and Restore:

System administrators often use .tar.gz files to back up important data and configurations. By compressing and archiving these files, they can easily store and transfer backups. In the event of data loss or system failure, they extract the backup archive to restore the data.

  1. Log Archiving:

Logs generated by servers and applications can consume significant disk space. Administrators periodically compress and archive older logs into .tar.gz files to save space and maintain a clean file system.

  1. Source Code Distribution:

Developers often distribute their source code in .tar.gz format. This allows users to download, extract, and build the software from source on their own systems.

  1. Data Transfer:

When transferring large datasets between systems or users, compressing the data into a .tar.gz file reduces the file size, making the transfer more efficient. The recipient can then extract the archive to access the data.

Conclusion and Key Points

Knowing how to extract or unzip .tar.gz files in Linux is an essential skill for efficiently managing software installations, backups, and data transfers. This process involves using the tar command with appropriate options to list and extract the contents of the archive. Alternative tools like gzip and gunzip provide additional methods for handling .tar.gz files, and graphical user interfaces offer a user-friendly approach for those less comfortable with the command line. Mastering these techniques ensures smooth software distribution, effective data management, and reliable backup solutions, enhancing overall productivity and system maintenance.

HAVE A QUESTION OR WANT TO GET A CUSTOM SOLUTION?

CONTACT SALES

FAQs

.tar.gz file is a compressed archive created using the tar command combined with gzip compression. It is commonly used for packaging multiple files into a single archive for easy distribution and storage.

To extract a .tar.gz file using the terminal, use the command: tar -xvzf [archive name]. This will extract the contents of the archive in the current directory.

The options -xvzf in the tar command stand for:

x: Extract files from the archive

v: Verbosely list files processed

z: Filter the archive through gzip

f: Use archive file

To extract a .tar.gz file to a specific directory, use the command: tar -xvzf [archive name] -C [target directory]. Replace [target directory] with the path to the directory where you want to extract the files.

To list the contents of a .tar.gz file without extracting it, use the command: tar -ztvf [archive name].

Yes, you can extract specific files from a .tar.gz archive by specifying the file names in the command: tar -xvzf [archive name] [file1] [file2].

If you encounter errors while extracting a .tar.gz file, check the file’s integrity, ensure you have the necessary permissions, and verify that you have enough disk space. You can also try using alternative extraction tools like gzip or gunzip.

To extract .tar.gz files using a GUI, locate the file in your file manager, right-click on it, and select the option to extract or unzip. Follow the prompts to choose the extraction location.

Common real-world use cases for .tar.gz files include distributing software packages, creating backups of important data, and transferring multiple files in a compressed format to save bandwidth and storage space.

To compress files into a .tar.gz archive, use the command: tar -czf [archive name] [file(s)/directory]. This will create a compressed archive from the specified files or directories.

Besides tar, you can use tools like gzip and gunzipto work with .tar.gz files. These tools provide additional options for compressing and decompressing files.

No, it’s not necessary to use the command line. You can also extract .tar.gz files using graphical tools available in most Linux file managers, which provide a user-friendly interface for extraction.