Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
04.08.2025

How to Unzip a .tar.gz File in Linux

Working with compressed files is routine in any Linux environment. Among the most commonly used formats is .tar.gz, which combines two utilities: tar for archiving and gzip for compression. This article dives into advanced usage and best practices for extracting .tar.gz files securely and efficiently.

 Understanding the .tar.gz Format

A .tar.gz file is a tarball archive that has been compressed using gzip. It is typically used for:

  • Software source code distribution
  • Backups and configuration packaging
  • Log file archiving

Example file: project-files.tar.gz

Basic Extraction Command

The most common way to extract a .tar.gz file is using the tar command with flags:

tar -xvzf file.tar.gz

Explanation:

  • x = extract
  • v = verbose (shows files being extracted)
  • z = decompress gzip
  • f = file name to work with

Extract to a Specific Directory

You can direct the extracted contents to a particular directory:

tar -xvzf file.tar.gz -C /path/to/target-directory

This is particularly useful in scripts or when organizing multiple archives.

Preview Contents Before Extracting

If you want to list the contents without extracting:

tar -tvzf file.tar.gz

This is a safety measure to avoid overwriting existing files or understanding the structure of the archive.

 Extract Specific Files

To extract a particular file or folder within the archive:

tar -xvzf file.tar.gz path/to/file.txt

You must specify the exact path as listed inside the archive (relative, not absolute).

Security Considerations

Be cautious when extracting untrusted .tar.gz files:

  • Use –no-overwrite-dir to prevent replacing directories.
  • Use –strip-components to avoid absolute path injections:
tar --strip-components=1 -xvzf malicious.tar.gz
Handling Large Archives

For archives exceeding several gigabytes:

  • Use pv (pipe viewer) to monitor progress:
pv file.tar.gz | tar xzvf -
  • Disable verbose mode for faster performance: tar -xzf file.tar.gz

 Decompress Manually in Two Steps

First unzip, then untar:

gunzip file.tar.gz # becomes file.tar tar -xvf file.tar

This offers better control when integrating into pipelines or inspecting intermediate files.

Using GUI Tools (Optional)

Desktop environments (GNOME, KDE) offer archive managers (e.g., File Roller, Ark) to handle .tar.gz with drag-and-drop simplicity.

Mastering .tar.gz files is essential for Linux power users. Whether automating server deployments, backing up logs, or unpacking software builds, tar offers flexibility and performance.

Always verify the contents, use flags appropriately, and handle unknown files with caution. For scripting, leverage -C, –strip-components, and piping with tools like pv to streamline workflows.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills