Learning how to open .gz and .tgz files in Linux is essential for managing source code releases, restoring system backups, and unpacking log bundles. Whether you’re extracting a downloaded software archive, analyzing compressed logs from remote servers, or deploying applications from tarball distributions, tar and gzip handle the heavy lifting. You will learn how to inspect archives before extraction, restore files safely to clean directories, extract specific files by pattern, and create your own compressed backups with proper permissions.
Understand GZ and TGZ Archives
If you are new to these archives, think of tar as the tool that bundles files together and gzip as the shrink wrap that squeezes the bundle down for faster transfers. TGZ files are simply tar archives that have been compressed with gzip, so you always touch both utilities when working with them.
tar [OPTIONS] -f ARCHIVE.tar.gz [PATHS...]
- tar: Runs the archiving program that packs or unpacks files.
- [OPTIONS]: Common flags include
-c(create),-x(extract),-z(gzip compression),-v(verbose), and-C(change directory). - -f ARCHIVE.tar.gz: Points tar to the archive you want to use. The
.tar.gzand.tgzextensions are interchangeable. - [PATHS…]: Optional list of files or directories to add or extract. Leaving this blank tells tar to include or extract everything.
The simplest extraction looks like tar -xzvf archive.tar.gz, which unpacks the archive into your current directory while printing each file name. Swap -x for -c to create a new archive instead.
gzip [OPTIONS] FILE
- gzip: Compresses or decompresses individual files.
- [OPTIONS]: Use
-dto decompress,-kto keep the original file, and-rto recurse into directories. - FILE: The file you want to compress or restore. The command adds
.gzwhen compressing and removes it when decompressing.
At its simplest, run gzip example.log to compress a single log and gunzip example.log.gz to bring it back.
| Task | Options | What They Do |
|---|---|---|
| Inspect a tar archive | tar -tzf archive.tar.gz | Lists files without extracting, so you can confirm paths first. |
| Extract a tar archive | tar -xzvf archive.tar.gz | Unpacks everything into the current directory while showing progress. |
| Extract to another location | tar -xzvf archive.tar.gz -C /tmp/app | Restores the archive into a different directory that you specify. |
| Decompress a single file | gunzip example.log.gz | Recreates the original file and removes the compressed copy. |
| Keep the compressed copy | gunzip -k example.log.gz | Writes the decompressed file but preserves the .gz version for later. |
In day-to-day administration you will often receive a single .gz file, such as a compressed log, or a .tgz archive that holds an entire project. The rest of this guide walks through both scenarios.
Check That Tar and Gzip Are Installed
Most Linux distributions ship with GNU tar and gzip pre-installed, but minimal containers or stripped-down servers might omit them. Check whether the binaries already exist before installing anything:
tar --version
gzip --version
If either command returns “command not found,” install or update the tools with your distro package manager.
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
Void Linux:
sudo xbps-install -S tar gzip
Gentoo Linux:
sudo emerge --ask app-arch/tar app-arch/gzip
Work with Single GZ Files
.gz files store one compressed file. You can peek at the contents or decompress them outright.
Preview Data without Extracting
Use the gzip helper utilities when you want to inspect a compressed file quickly:
- Display contents:
zcat example.log.gzstreams the decompressed output to stdout. - Page through long files:
zless example.log.gzopens the file with the familiarlessinterface. - Search inside:
zgrep -i error example.log.gzscans the compressed file without unpacking it first.
These gzip utilities (zcat, zless, zgrep) work with any .gz file and are perfect for quick inspections without permanent extraction. zcat can pipe output to other tools like head or grep, while zgrep supports full regex patterns for advanced searches. For more on using grep patterns and searches, see our dedicated guide.
Decompress a GZ File
gunzip restores the original file and removes the compressed copy. Use -k when you want to keep both versions.
# Restore the original file and delete example.log.gz
gunzip example.log.gz
# Restore the file but keep a compressed copy
gunzip -k example.log.gz
You can achieve the same effect with gzip -d and combine it with globbing to decompress several files in one command:
# Decompress every .gz file in the current directory
gzip -dk *.gz
Extract TGZ (tar.gz) Archives
Tar archives can hold entire directory trees. Combine tar with gzip to shrink those archives for faster downloads or backups.
List the Archive Contents First
Always check what is inside before extracting to avoid overwriting files unexpectedly.
# Show files in the archive with permissions and ownership
tar -tzvf archive.tar.gz
Add | less to page through long listings or | grep pattern to filter specific files. For more on grep filtering techniques, see our dedicated guide.
Extract to the Current Directory
Use the -x (extract), -z (gzip), -v (verbose), and -f (file) flags together to unpack the archive.
# Extract everything into the working directory
tar -xzvf archive.tar.gz
Tar preserves file permissions by default. Run the command with sudo if you need to restore ownership owned by root.
Extract to a Different Location
The -C flag changes the extraction directory. Create the target folder ahead of time if it does not exist.
mkdir -p /srv/backups/app
tar -xzvf archive.tar.gz -C /srv/backups/app
For more details on creating directories with the -p flag, see our mkdir command guide.
Extract Only Specific Files
Combine tar -x with a file path to extract a single item. Use --strip-components when you want to discard leading directory levels from the archive path.
# Pull one file out of the archive without restoring the entire tree
tar -xzvf archive.tar.gz --strip-components=2 path/in/archive/config.yaml
This approach is handy when an archive contains a long prefix such as project-1.0.0/ and you only need a single configuration file.
Extract Files by Pattern
When you need to extract multiple files matching a specific pattern, use the --wildcards option. This is useful for pulling out all files of a certain type without listing them individually.
# Extract all .txt files from the archive
tar -xzvf archive.tar.gz --wildcards '*.txt'
Always quote the wildcard pattern to prevent the shell from interpreting it before tar processes the command.
Extract Archives with a GUI
If you prefer a graphical interface, most Linux desktop environments ship archive managers that understand .tar.gz files.
- Right-click the archive in your file manager (Nautilus, Dolphin, Thunar, or similar tools work the same way).
- Choose “Extract Here” to unpack in the current directory, or “Extract to…” to send the files elsewhere.
- The archive manager creates a folder named after the archive and restores the files while preserving permissions.
This approach is convenient for occasional tasks, though command-line extraction scales better on servers or in scripts.
Extract from Standard Input
Piping a remote download straight to tar lets you unpack files without storing the archive first, which saves disk writes on remote hosts.
# Download and extract using wget
wget -qO - https://example.com/archive.tar.gz | tar -xz
# Or with curl
curl -s https://example.com/archive.tar.gz | tar -xz
If you forget the -z flag and the stream is compressed, tar exits with an error instead of extracting, so rerun the command with gzip enabled. For more advanced wget or curl download techniques, see our dedicated command guides.
Troubleshooting Common Issues
Even with the right commands, you might encounter errors when working with GZ and TGZ files. Here are solutions to frequent problems.
- Archive not recognized: If
tarsays “This does not look like a tar archive,” the file might be corrupted, not gzip-compressed, or have the wrong extension. Check withfile archive.tar.gz. If it’s plain text or another format, use the appropriate tool (e.g.,unzipfor ZIP files). - Not in gzip format: For single GZ files,
gziporgunzipmight report “not in gzip format.” Verify the file type withfile example.gz. If it’s not compressed, you may not need to decompress it. - Permission denied: Extraction fails with permission errors if you lack write access to the target directory or if the archive contains root-owned files. Use
sudofor system-wide extractions or extract to a user-writable location with-C /path/to/dir. - Insufficient disk space: Large archives might fail to extract if there’s not enough space. Check available space with
df -hand free up room or extract to another filesystem. - Corrupted archive: If extraction stops midway or produces gibberish, the file may be damaged. Redownload if possible, or use
tar -tzf archive.tar.gzto test integrity without extracting.
Create Your Own TGZ Archives
Use tar’s create flag (-c) alongside gzip compression when you want to package files for backup or distribution.
# Create archive.tar.gz from the contents of the project directory
tar -czvf archive.tar.gz project/
Place the -C flag before the directory name if you want to change into another location during archive creation:
# Change to /var/www first, then archive the app directory
tar -czvf app-backup.tgz -C /var/www app
Tar supports exclusions. For example, skip Git metadata and large cache folders while building release archives:
tar -czvf app-release.tgz project/ --exclude='project/.git' --exclude='project/tmp'
Verify and Maintain Archives
After creating or downloading archives, a few quick checks prevent surprises later:
- Confirm integrity:
tar -tzf archive.tar.gzensures the archive can be read without extracting it. - Check file types:
file archive.tar.gzidentifies whether a file is a plain Gzip stream or a tar archive. - Verify downloads: Compare hashes with
sha256sum archive.tar.gzwhen the publisher provides checksum files.
Differences Between Archive Formats
While .gz and .tgz are common in Linux, understanding how they compare to other formats helps you choose the right tool for backups or data transfer.
- TAR: Archives multiple files without compression. Preserves permissions and metadata but doesn’t reduce size.
- GZ: Compresses a single file using gzip. Efficient for logs or text files, but not for bundling multiple items.
- TGZ/TAR.GZ: Combines tar archiving with gzip compression. Ideal for software distributions and backups, balancing size and structure.
- ZIP: Cross-platform format that archives and compresses files individually. Common on Windows, supports encryption but may create larger archives than TGZ for some data types.
- BZ2/TAR.BZ2: Uses bzip2 compression, often better for large files but slower than gzip.
- XZ/TAR.XZ: High compression with xz, great for space-constrained environments, though extraction can be slower.
For most Linux tasks, TGZ strikes the best balance of speed, compression, and compatibility.
Conclusion
Opening and managing .gz and .tgz files with tar and gzip gives you reliable control over software releases, system backups, and compressed logs. By inspecting archives before extraction with tar -tzf, extracting safely to specific directories with -C, pulling individual files by pattern with --wildcards, and creating your own releases with proper exclusions and permissions, you can handle upstream distributions, automated deployments, and support bundles without surprises. Keep these command patterns close at hand and you will confidently manage any compressed archive that crosses your path.
For extracting other archive formats like zip, see our unzip commands guide.