How to Open .gz and .tgz Files in Linux

Open .gz and .tgz files in Linux with tar and gzip commands. Extract, inspect archives, pull specific files, and create your own backups.

Last updatedAuthorJoshua JamesRead time8 minGuide typeLinux Commands

Compressed Linux downloads, backup bundles, and rotated logs often arrive as .gz, .tgz, or .tar.gz files, but those extensions do not all open the same way. A .gz file is one compressed file, while a .tgz or .tar.gz file is a tar archive compressed with gzip. To open .gz and .tgz files in Linux safely, identify the file type first, list archive contents before extraction, then unpack to a clean directory with gzip or tar.

The command examples use GNU tar and GNU gzip, which are standard on most full Linux distributions. Minimal containers and Alpine systems may start with BusyBox applets instead; those work for basic extraction but can miss GNU-only options such as --wildcards.

Understand GZ and TGZ Files in Linux

Think of tar as the tool that bundles files and directories together, and gzip as the compressor that shrinks one stream. That difference matters because gunzip can restore a single .gz file, but a .tgz file still needs tar to unpack the directory tree inside it.

Use these common patterns as a quick reference before moving into the full examples:

TaskCommand PatternWhat It Does
Identify an unknown filefile archive.tgzReports the real file format before you choose a tool.
List a TGZ archivetar -tzf archive.tgzShows archive paths without extracting files.
Extract a TGZ archivetar -xzf archive.tgz -C output-dirUnpacks the archive into a directory you control.
Read a single GZ filezcat example.log.gzStreams decompressed text to the terminal.
Decompress a GZ filegunzip -k example.log.gzRestores the original file while keeping the compressed copy.

The phrase “unzip a TGZ file” is common search shorthand, but unzip is for ZIP archives. Use tar -xzf for .tgz and .tar.gz files; use unzip commands for ZIP archives.

Verify File Types Before Opening GZ or TGZ Files

Start with file when an archive comes from a browser download, email attachment, support bundle, or unknown source. Extensions can be wrong, and a failed download may leave an HTML error page named like an archive.

file archive.tar.gz

Relevant output for a real gzip-compressed tar archive looks like this:

archive.tar.gz: gzip compressed data, from Unix, original size modulo 2^32 10240

If the output says Zip archive data, switch to unzip. If you see an unfamiliar extension such as .tgx, do not assume it is a typo for .tgz; confirm the format first.

Check Tar and Gzip Availability

Normal Linux installations usually include tar and gzip. Check the active binaries before installing packages on a minimal server or container image:

command -v tar gzip

Expected output shows both commands on your PATH:

/usr/bin/tar
/usr/bin/gzip

Check the tool family when GNU-only options matter:

tar --version | head -n 1
gzip --version | head -n 1

GNU tools print a short version line:

tar (GNU tar) 1.35
gzip 1.14

If either command is missing, install the packages with the manager for your distribution. Omit sudo when you are already working as root, which is common inside containers.

Ubuntu and Debian-based distributions:

sudo apt update
sudo apt install tar gzip

Fedora, RHEL, AlmaLinux, and Rocky Linux:

sudo dnf install tar gzip

Arch Linux, Manjaro, and other pacman-based systems:

sudo pacman -Syu tar gzip

openSUSE Leap and Tumbleweed:

sudo zypper install tar gzip

Alpine Linux:

sudo apk add tar gzip

Alpine includes BusyBox applets on small images. Installing the tar package gives you GNU tar, which is useful when examples depend on options such as --wildcards.

Void Linux:

sudo xbps-install -S tar gzip

Gentoo Linux:

sudo emerge --ask app-arch/tar app-arch/gzip

Open Single GZ Files in Linux

A plain .gz file contains one compressed stream, not a directory tree. Log files are the most common example: example.log.gz restores to example.log.

Read a GZ File Without Decompressing It

The gzip helper commands let you inspect compressed text while leaving the file in place. Use zcat to stream content to another command, zgrep to search without creating a temporary copy, and zless when you want an interactive pager for a long file.

zcat example.log.gz | head

For a short log file, the first lines appear as normal text:

alpha error
beta ok

Search the compressed file directly when you only need matching lines:

zgrep -i "error" example.log.gz

The matching zgrep search prints only the relevant line:

alpha error

zgrep accepts normal grep patterns and flags, so the grep command examples also help when you need case-insensitive searches, fixed-string matches, or context lines inside compressed logs.

Decompress a GZ File with Gunzip

gunzip removes the compressed .gz file after a successful decompression unless you add -k.

Restore the original file and keep the compressed copy when you may need to retry, compare, or share the archive again:

gunzip -k example.log.gz

Check that both files exist:

ls -1 example.log*
example.log
example.log.gz

gzip -d is the same decompression operation. Add -k and a glob when you want to decompress several gzip files while preserving the compressed versions:

gzip -dk *.gz

Extract TGZ and tar.gz Archives in Linux

A .tgz file and a .tar.gz file are the same archive format. The short .tgz extension is just an older, compact name for a gzip-compressed tar archive.

List TGZ Archive Contents Before Extraction

List an unfamiliar archive before extracting it. This catches unexpected top-level paths and reduces the chance of overwriting files in the current directory.

Use -t to list files, -z for gzip compression, and -f to name the archive:

tar -tzf archive.tar.gz
project/
project/config.yaml
project/readme.txt

Add -v as tar -tzvf archive.tar.gz when you also need permissions, owner names, sizes, and modification times. For long archives, pipe the list through less or filter it with grep.

Extract a TGZ Archive to a Clean Directory

Create the destination directory first, then extract with -C. This keeps the restored files out of your current working directory.

mkdir -p extract-project
tar -xzvf archive.tar.gz -C extract-project

The verbose flag prints each extracted path:

project/
project/config.yaml
project/readme.txt

The mkdir command guide covers -p and nested directory creation in more detail. Use sudo only when extracting into a system-owned location or restoring root-owned ownership intentionally.

Extract Specific Files from TGZ Archives

Name an archive member after the archive file when you need only one path. --strip-components=1 removes the leading project/ directory in this example, so the file lands directly in extract-config.

mkdir -p extract-config
tar -xzvf archive.tar.gz -C extract-config --strip-components=1 project/config.yaml
project/config.yaml

Verify the restored file path:

find extract-config -type f -print
extract-config/config.yaml

Extract TGZ Files by Pattern

GNU tar supports --wildcards when you want to extract matching paths. Quote the pattern so your shell does not expand it before tar reads the archive list.

mkdir -p extract-text
tar -xzvf archive.tar.gz -C extract-text --wildcards 'project/*.txt'
project/readme.txt

BusyBox tar does not support --wildcards. On Alpine or another BusyBox-based environment, install GNU tar first or list the exact archive member names.

Extract TGZ Archives from Standard Input

Streaming a trusted download directly into tar can save disk space on temporary hosts, but it also skips the chance to inspect or checksum the downloaded file first. Save the archive locally when integrity matters. Set ARCHIVE_URL to the real archive URL before using this pattern.

mkdir -p extract-project
curl -fsSL "$ARCHIVE_URL" | tar -xz -C extract-project

The -z flag is required for gzip-compressed data that arrives through standard input. GNU tar can auto-detect compression from a named file, but a pipe has no useful filename suffix. The curl command guide and wget command examples cover safer download options, redirects, retries, and output files.

Extract TGZ Archives with a GUI

Desktop file managers usually hand .tar.gz and .tgz files to an archive manager. Right-click the archive, choose the extract action, and select a destination folder. The terminal remains better for servers, repeated tasks, and archives where you need to inspect exact paths first.

Create TGZ Archives in Linux

Use -c to create an archive, -z to compress with gzip, and -f to name the output file. Add -v when you want tar to print each path it adds.

tar -czvf app-backup.tgz project/
project/
project/config.yaml
project/readme.txt

Place -C before the source directory when you want archive paths to start inside a different location:

tar -czf app-backup.tgz -C /var/www app

Skip generated files, Git metadata, and cache directories with --exclude. Put exclusions before the source path so GNU tar sees them before it descends into the directory tree.

tar -czf app-release.tgz --exclude='project/.git' --exclude='project/tmp' project/

Maintain and Secure TGZ Archives

A few checks make archived backups and downloaded release bundles easier to trust later. Test readability, keep checksums beside important downloads, and split large files only when the receiving system needs smaller parts.

Test TGZ Archive Integrity

Use tar -tzf for archive readability and gzip -t for gzip-stream integrity. Add an echo success message after each command if you want visible confirmation when the test passes.

tar -tzf archive.tar.gz >/dev/null && echo "archive.tar.gz: readable"
gzip -t archive.tar.gz && echo "archive.tar.gz: gzip stream OK"
archive.tar.gz: readable
archive.tar.gz: gzip stream OK

Verify TGZ Archives with Checksums

For archives you create, save a SHA256 checksum beside the file so you can verify it later before extraction.

sha256sum archive.tar.gz > archive.tar.gz.sha256
sha256sum --check archive.tar.gz.sha256
archive.tar.gz: OK

For downloaded release archives, use the checksum supplied by the publisher instead of generating your own after the download. Place that trusted checksum and filename in the same two-column format before running sha256sum --check.

Split Large TGZ Archives

split is useful when a filesystem, upload form, or storage service has a file-size limit. Use numbered suffixes with a fixed width so shell globbing reassembles the parts in the correct order.

split -b 1G -d -a 3 archive.tar.gz archive.tar.gz.part-

Reassemble the parts into a new archive, then list it before extraction:

cat archive.tar.gz.part-* > archive-restored.tar.gz
tar -tzf archive-restored.tar.gz
project/
project/config.yaml
project/readme.txt

The split command guide covers line-based splits, byte-size suffixes, and safer naming patterns for larger workflows.

Know What Tar and Gzip Do Not Provide

Standard tar and gzip do not add password protection. Create the archive first, then encrypt the finished file with a separate tool such as GnuPG, or choose an archive format with built-in encryption when that compatibility matters.

gzip is also not a parallel compressor. For CPU-bound compression on large archives, pigz is the common parallel gzip replacement, but it is a separate utility. Make that a deliberate dependency instead of assuming every target system has it.

Compare TGZ with Other Archive Formats

Different archive formats solve different problems. Choose the format based on whether you need one compressed file, a whole directory tree, stronger compression, encryption, or broad cross-platform support.

FormatStores Multiple FilesCompressionBest Fit
.tarYesNo compressionPreserving Linux paths, permissions, and ownership without shrinking data.
.gzNogzipCompressing a single log, dump, or stream.
.tgz / .tar.gzYestar plus gzipLinux source releases, backups, and support bundles.
.zipYesPer-file ZIP compressionSharing archives across Linux, Windows, and macOS.
.tar.bz2Yesbzip2Better compression than gzip when slower processing is acceptable.
.tar.xzYesxzHigh compression for source releases and package archives.

GNU tar can auto-detect compression from named archive files, so tar -xf archive.tar.xz often works. BusyBox tar is stricter in practice; use explicit flags such as -xzf for gzip, -xjf for bzip2, and -xJf for xz when working on minimal systems.

Troubleshoot GZ and TGZ Errors

Most archive failures come from a wrong file type, a truncated download, missing permissions, or a reduced tar implementation. Match the exact error first, then choose the smallest fix.

Fix “not in gzip format” Errors

A failed download, wrong extension, or non-gzip file can trigger this error:

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

Check the real format before retrying:

file archive.tar.gz

If file reports HTML, plain text, ZIP, or another format, download the correct archive or switch tools. Verify a real TGZ archive with a list command:

tar -tzf archive.tar.gz

Fix “unexpected end of file” Errors

A truncated archive usually fails with gzip and tar errors together:

gzip: stdin: unexpected end of file
tar: Child returned status 1
tar: Error is not recoverable: exiting now

Test the gzip stream directly:

gzip -t archive.tar.gz

No output means the gzip layer passed. If the same error returns, redownload the archive, compare checksums, or ask the sender to recreate it.

Fix Permission Denied During TGZ Extraction

Extraction fails when you cannot write to the destination directory:

tar: project: Cannot mkdir: Permission denied
tar: Exiting with failure status due to previous errors

Extract into a user-writable directory, or intentionally use sudo only for system-owned destinations:

mkdir -p "$HOME/extract-project"
tar -xzf archive.tar.gz -C "$HOME/extract-project"

Check disk space as well when extraction stops partway through:

df -h "$HOME"

Use du disk usage analysis examples when you need to find which directories are consuming the space.

Fix BusyBox or Alpine tar.xz Extraction Problems

On minimal systems, BusyBox tar may fail or appear unhelpful if you omit the compression flag for .tar.xz files:

tar: short read

Use the explicit xz flag:

tar -xJf archive.tar.xz

Install GNU tar on Alpine when you need GNU-specific options or friendlier archive auto-detection:

sudo apk add tar gzip

Verify the active implementation afterward:

tar --version | head -n 1
tar (GNU tar) 1.35

Fix Single GZ Files That Contain More Than One Entry

A plain .gz stream is meant to restore one file. If gzip reports that standard input has more than one entry, you may be feeding it concatenated gzip members or using gzip where tar should own the bundle.

gzip: stdin has more than one entry--rest ignored

Use file to confirm the format. For a real .tar.gz or .tgz archive, extract with tar instead of treating it like a single gzip file:

file archive.tgz
tar -tzf archive.tgz

Conclusion

tar and gzip cover the common Linux archive workflow: inspect the file type, list paths before extraction, unpack into a controlled directory, and verify important downloads before trusting them. Once those habits are in place, .gz, .tgz, .tar.gz, and related tar formats become predictable instead of risky.

Share this guide

Help another Linux user troubleshoot faster

Share this guide with someone troubleshooting Linux systems or saving it for later.

Follow LinuxCapable

Want more LinuxCapable guides in Google?

Add LinuxCapable as a preferred source so Google can show more of our fresh Linux tutorials in Top Stories and From your sources when relevant.

Add LinuxCapable as a preferred source on Google
Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffeeBuy me a coffee
Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags currently allowed in published comments:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<blockquote>quote</blockquote> quote block

Got a Question or Feedback?

We read and reply to every comment - let us know how we can help or improve this guide.

Verify before posting: