| Tested on | Red Hat Enterprise Linux 10.2 (lab VM) |
|---|---|
| Package | tar (GNU tar) 1.35gzip 1.13 |
| Applies to | RHEL, Rocky Linux, AlmaLinux, CentOS Stream, Fedora, Debian, Ubuntu, and other GNU/Linux systems with GNU tar and gzip |
| Privilege | Normal user (read archives you can open; write where you have permission) |
| Scope | Full extraction of .tar.gz and .tgz archives; decompress plain .gz files; list archive members; extract to another directory; view gzip streams without writing files. Does not cover selective file extraction or recursive bulk unpacking. |
| Related guides | tar command Extract specific files from tar.gz tar --strip-components Linux file management gzip command |
A .tar.gz file is a tar archive wrapped in gzip compression. A plain .gz
file is usually one compressed file, not an archive at all. Mixing the two leads
to the wrong command and confusing errors, so this guide keeps them separate
from the first section onward.
You will see .tar.gz on source tarballs, container image layers, and backup
bundles that must preserve paths and permissions. You will see plain .gz on
rotated logs, single-file exports, and gzip-compressed payloads that are not
tar archives at all.
Quick Reference: tar.gz vs .gz
| File type | What it contains | Tool | Typical extract command |
|---|---|---|---|
.tar.gz / .tgz |
Many files and directories in a tar archive, gzip-compressed | GNU tar |
tar -xf archive.tar.gz |
.gz |
One gzip-compressed stream (often a single file) | gunzip / gzip -d |
gunzip file.gz |
GNU tar 1.35 on current GNU/Linux can read .tar.gz with tar -xf alone and
detect gzip automatically. tar -xzf remains valid when you want to name the
compression filter explicitly.
Difference Between .gz, .tar.gz, and .tgz
Gzip compresses a single byte stream. When you gzip one file, you get
document.pdf.gz. When you gzip a tar archive that already bundles many paths,
you get release.tar.gz.
| Extension | Structure | Opens with |
|---|---|---|
.gz |
One compressed stream | gunzip, gzip -d, zcat |
.tar.gz |
tar archive + gzip wrapper | tar -xf (GNU tar auto-detects gzip) |
.tgz |
Same as .tar.gz |
tar -xf |
Both .tar.gz and plain .gz files can be identified as gzip-compressed data
by file. That output alone does not prove the payload is a tar archive.
Do not run tar -xf on a plain .gz log or config backup and expect a folder
tree. Use gunzip, gzip -d, or zcat for known single-file gzip payloads.
If you need one member from a .tar.gz without unpacking everything, list
members with tar -tf and pass the exact stored path to tar -xf for a partial
extract.
Prepare lab archives
The examples below use a disposable directory under /tmp/tar-gz-lab. Create the
sample tree and archives once:
mkdir -p /tmp/tar-gz-lab/demoAdd two demo files the archive will bundle:
printf 'hello from demo\n' > /tmp/tar-gz-lab/demo/readme.txtAdd a second config file:
printf 'config line\n' > /tmp/tar-gz-lab/demo/app.confPack the demo/ directory into release.tar.gz:
tar -czf /tmp/tar-gz-lab/release.tar.gz -C /tmp/tar-gz-lab demo/Copy the same archive as bundle.tgz for the .tgz example later:
cp /tmp/tar-gz-lab/release.tar.gz /tmp/tar-gz-lab/bundle.tgzBuild a plain .gz file for the type-check examples later in this guide:
printf 'hello\n' | gzip -c > /tmp/tar-gz-lab/plain.gzCreate stream.txt.gz for the read-only gzip examples:
printf 'stream read\n' | gzip -c > /tmp/tar-gz-lab/stream.txt.gzConfirm a gzip file contains a tar archive
Run file when you only know the download is gzip-compressed:
file /tmp/tar-gz-lab/plain.gz /tmp/tar-gz-lab/release.tar.gzSample output:
/tmp/tar-gz-lab/plain.gz: gzip compressed data, from Unix, original size modulo 2^32 6
/tmp/tar-gz-lab/release.tar.gz: gzip compressed data, from Unix, original size modulo 2^32 10240Both lines say gzip compressed data, so file cannot choose between tar and
non-tar payloads. List members with tar -tf when you expect a directory tree:
tar -tf /tmp/tar-gz-lab/release.tar.gzSample output:
demo/
demo/readme.txt
demo/app.confOn a valid tar archive, tar prints member paths. Test a known plain .gz the
same way:
tar -tf /tmp/tar-gz-lab/plain.gzGNU tar does not list any members because the decompressed payload is not tar
data. Depending on version and warning settings, tar may report an
archive-format problem (for example Terminating zero blocks missing at 0) or
exit without listing paths. Either outcome means you should use gunzip,
gzip -d, or zcat instead of tar.
When you still see tar -xzf
Scripts and older tutorials spell out tar -xzf because -z means “filter
through gzip.” On current GNU tar, both forms unpack the same .tar.gz archive:
| Form | Meaning on GNU tar 1.35 |
|---|---|
tar -xf archive.tar.gz |
Auto-detect gzip while reading |
tar -xzf archive.tar.gz |
Explicitly pipe through gzip |
On current GNU tar, tar -xf archive.tar.gz automatically detects gzip
compression. tar -xzf remains valid and is still useful in scripts where you
want to state the compression method explicitly or where portability to non-GNU
tar implementations matters.
Extract a .tar.gz File with tar
Use the release.tar.gz archive from Prepare lab archives. Change into the
lab directory:
cd /tmp/tar-gz-labExtract every member into the current working directory:
tar -xf release.tar.gzGNU tar prints nothing on success when you omit -v. List what landed:
find demo -type fSample output:
demo/readme.txt
demo/app.confThe long form tar -xzf release.tar.gz still works. The -z flag tells GNU tar to
pipe through gzip explicitly. On GNU tar 1.35, tar -xf alone auto-detects gzip
for .tar.gz archives.
Add -v when you want tar to print each path as it extracts:
tar -xvf release.tar.gzSample output:
demo/
demo/readme.txt
demo/app.confFor more flags and archive creation examples, run tar --help or read man tar.
Tar preserves the paths, timestamps, and permission bits stored in the archive
header. If extracted files look executable when you did not expect that, check
what the packager stored with tar -tvf before you run anything from the
unpack directory.
Extract .tar.gz to Another Directory
By default, tar recreates the stored directory tree under your current path. Create the destination directory first:
mkdir -p /tmp/tar-gz-lab/extractedPoint tar at that folder with -C before it writes any members:
tar -xf release.tar.gz -C /tmp/tar-gz-lab/extractedTar places the demo/ directory from the archive under extracted/. Confirm
with:
ls /tmp/tar-gz-lab/extracted/demo/Sample output:
app.conf
readme.txtThe -C directory must exist before you run tar. Tar does not create missing
parent paths for you.
You can place -C before or after the archive name on GNU tar as long as the
next argument is the destination path. This equivalent form names the destination
first:
tar -C /tmp/tar-gz-lab/extracted -xf release.tar.gzBoth -C placements leave the same tree under extracted/demo/.
List .tar.gz Contents Without Extracting
Before you unpack a large download, list members with -t and skip extraction:
tar -tf release.tar.gzSample output:
demo/
demo/readme.txt
demo/app.confAdd -v for permission, owner, and size columns (tar -tvf). The paths tar
prints are the exact strings you need when extracting individual members from a
partial unpack.
Listing is cheap compared to writing thousands of small files to disk. On a
server with limited free space, run tar -tf first to confirm the archive
contains what you expect before you extract.
Extract a .tgz Archive
A .tgz file is a gzip-compressed tar archive, identical to .tar.gz. Only
the filename extension differs. Software vendors use .tgz when they want a
shorter name; the commands are the same.
Prepare an output directory:
mkdir -p /tmp/tar-gz-lab/tgz-outRun the same extract command you would use for .tar.gz:
tar -xf bundle.tgz -C /tmp/tar-gz-lab/tgz-outList the result:
ls -R /tmp/tar-gz-lab/tgz-outSample output:
/tmp/tar-gz-lab/tgz-out:
demo
/tmp/tar-gz-lab/tgz-out/demo:
app.conf
readme.txtDecompress a Plain .gz File with gunzip
Plain .gz files need gunzip or gzip -d, not tar. The command replaces
file.gz with the decompressed file by default.
Build a small example archive to decompress:
printf 'plain gzip test\n' | gzip -c > /tmp/tar-gz-lab/notes.txt.gzRun gunzip on the compressed file:
gunzip /tmp/tar-gz-lab/notes.txt.gzgunzip removes the .gz file after a successful run. Read the result:
cat /tmp/tar-gz-lab/notes.txtSample output:
plain gzip testgzip -d is equivalent to gunzip. By default, decompression replaces the
.gz file with the decompressed file, normally removing the recognized gzip
suffix from the filename. For notes.txt.gz, that writes /tmp/tar-gz-lab/notes.txt.
If you batch-decompress many .gz files in one directory, gunzip *.gz expands
each match in turn. That is separate from unpacking a .tar.gz, which is still
a single tar operation on one archive file.
Keep the Original .gz File After Decompression
By default, gunzip deletes the compressed file. Pass -k (keep) to leave the
.gz in place.
Create and compress a file:
printf 'keep me\n' > /tmp/tar-gz-lab/keep.txtThe -n flag on gzip skips storing the original filename in the header, which
keeps tiny test files predictable:
gzip -n /tmp/tar-gz-lab/keep.txtDecompress while keeping the .gz copy:
gunzip -k /tmp/tar-gz-lab/keep.txt.gzBoth files should exist now:
ls -l /tmp/tar-gz-lab/keep.txt*Sample output:
-rw-r--r--. 1 root root 8 Aug 15 18:05 /tmp/tar-gz-lab/keep.txt
-rw-r--r--. 1 root root 28 Aug 15 18:05 /tmp/tar-gz-lab/keep.txt.gzRead a .gz File Without Extracting It
When you only need to peek at compressed text, stream it to stdout instead of writing a new file on disk.
zcat (or gzip -dc) prints the decompressed bytes to the terminal:
zcat /tmp/tar-gz-lab/stream.txt.gzSample output:
stream readgzip -l shows compressed size, uncompressed size, and ratio without
decompressing to a separate file:
gzip -l /tmp/tar-gz-lab/stream.txt.gzSample output:
compressed uncompressed ratio uncompressed_name
30 12 0.0% stream.txtFor long logs, pipe to less: zcat big.log.gz | less.
gunzip -c behaves like zcat and is handy in scripts when you want stdout
without touching the .gz file on disk:
gunzip -c /tmp/tar-gz-lab/stream.txt.gz | wc -lSample output:
1One line means the compressed file held a single line of text after decompression.
Common tar.gz and gunzip Errors
| Symptom | Likely cause | Fix |
|---|---|---|
tar: archive.tar.gz: Cannot open: No such file or directory |
Wrong path or typo | Run ls on the archive path; use tab completion |
gzip: file.gz: No such file or directory |
Missing .gz file |
Confirm the filename with ls *.gz |
gzip: file.txt already exists; not overwritten |
Decompressed file already present | Remove or rename the existing file, or use gunzip -f to force |
tar -tf lists no members on a .gz file |
Payload is plain gzip, not tar | Use gunzip, gzip -d, or zcat instead of tar |
tar: Unexpected EOF in archive |
Truncated or corrupt download | Re-download; verify checksum if provided |
Missing archive paths fail fast:
tar -xf /tmp/tar-gz-lab/missing.tar.gzSample output:
tar: /tmp/tar-gz-lab/missing.tar.gz: Cannot open: No such file or directory
tar: Error is not recoverable: exiting nowWhen gunzip refuses to overwrite an existing file, either remove the target
or pass -f. The collision happens when the decompressed filename already
exists on disk:
printf 'y\n' > /tmp/tar-gz-lab/dup.txtCompress it so only dup.txt.gz remains:
gzip -n /tmp/tar-gz-lab/dup.txtRecreate the plaintext file so gunzip has something to collide with:
printf 'y\n' > /tmp/tar-gz-lab/dup.txtTry decompression without -f:
gunzip /tmp/tar-gz-lab/dup.txt.gzSample output:
gzip: /tmp/tar-gz-lab/dup.txt already exists; not overwrittengunzip -f forces overwrite when you are sure the existing file is safe to
replace.
References
- GNU tar manual
- gzip manual
man tarman gzip
Summary
A .tar.gz or .tgz file is a tar archive inside gzip compression. On GNU tar,
extract the whole archive with tar -xf archive.tar.gz; current GNU tar detects
gzip without -z, though tar -xzf still works when you want the filter named
explicitly or need compatibility with non-GNU tar. Use -C /destination when you
want the tree somewhere other than the current directory, and tar -tf when you
only need a member list before unpacking.
A plain .gz file is different: it wraps one compressed stream, so reach for
gunzip or gzip -d instead of tar. Add gunzip -k when you want to keep the
.gz after decompression, and use zcat or gzip -l to read or inspect
compressed data without writing a new file.
When you need a single file or subdirectory from a .tar.gz, extract only those
members instead of unpacking the full archive and deleting what you do not need.
On shared servers, extract as a normal user into a directory you own (for
example ~/downloads/ or /tmp/$USER/). You only need root when the
destination path or extracted permissions require elevated access. Check
ownership and permission bits after you unpack third-party tarballs.

