.gzis one compressed file..tar.gzis many files bundled together and then compressed. They need different commands.- Extract a
.gzwithgunzip file.gz, which replaces the original. Usegzip -dk file.gzto keep it. - Extract a
.tar.gzwithtar -xzf archive.tar.gz, and add-C /target/dirto control where it lands. - Always list the contents first with
tar -tzf: an archive without a top-level folder will scatter files across your current directory. - You need roughly three to four times the archive size in free disk space before extracting a compressed database dump.
What is the difference between .gz and .tar.gz?
gzip compresses exactly one file. It has no concept of folders or multiple files. tar does the opposite: it bundles many files into one archive but compresses nothing. .tar.gz is the two combined, tar first and gzip second, which is why the extension has two parts and why the commands differ.
| Extension | What it is | Extract with |
|---|---|---|
| .gz | A single compressed file | gunzip file.gz |
| .tar | Many files bundled, not compressed | tar -xf archive.tar |
| .tar.gz or .tgz | Many files bundled, then compressed | tar -xzf archive.tar.gz |
| .sql.gz | A compressed database dump (a single file) | gunzip dump.sql.gz |
| .tar.bz2 | Bundled, compressed with bzip2 | tar -xjf archive.tar.bz2 |
| .tar.xz | Bundled, compressed with xz | tar -xJf archive.tar.xz |
The letter in the tar flag names the compressor: z for gzip, j for bzip2, J for xz.
Modern tar detects the compression automatically, so tar -xf archive.tar.gz works without the z. Type the letter anyway. It documents what the file is, and it still matters on older systems and in scripts you may run anywhere.
How do I extract a .gz file?
gunzip file.gz decompresses in place and removes the .gz. Note the removal: by default the compressed original is gone once it has been unpacked. If you want to keep it, use the -k flag.
# Standard: replaces backup.sql.gz with backup.sql
gunzip backup.sql.gz
# Keep the compressed original too
gzip -dk backup.sql.gz
# Write the output somewhere else, original untouched
gunzip -c backup.sql.gz > /var/tmp/backup.sql
# Read it without extracting at all
zcat backup.sql.gz | head -50
zless backup.sql.gz
zgrep 'CREATE TABLE' backup.sql.gzCheck the free space first
A 2 GB compressed database dump can be 15 GB uncompressed. Extracting it on a server with 10 GB free will fill the disk, and a full disk takes down the database and the site with it. Run df -h . before you start.
# gzip reports the uncompressed size without extracting anything
gzip -l backup.sql.gz
# Free space where you are about to write it
df -h .gzip -l reads the size stored in the file footer, so it is instant even on a very large archive.How do I extract a .tar.gz file?
tar -xzf archive.tar.gz unpacks into the current directory. The three flags are extract, gzip and file. Unlike gunzip, tar leaves the archive in place, so there is nothing to preserve.
# Extract here
tar -xzf archive.tar.gz
# Extract into a specific directory (create it first)
mkdir -p /var/www/restore
tar -xzf archive.tar.gz -C /var/www/restore
# Show each file as it is extracted
tar -xzvf archive.tar.gz
# Extract a single file out of a large archive
tar -xzf archive.tar.gz wp-content/uploads/2026/logo.png
# Extract everything matching a pattern
tar -xzf archive.tar.gz --wildcards '*.sql'-C changes directory before extracting, so the path applies to the destination and not to where you happen to be standing.List before you extract
Some archives contain a single top-level folder and unpack tidily. Others contain hundreds of loose files that will land directly in your current directory, mixed in with whatever was already there. tar -tzf archive.tar.gz | head tells you which kind you have, and takes a second.
# First 20 entries: is there a single top-level folder?
tar -tzf archive.tar.gz | head -20
# How many files in total?
tar -tzf archive.tar.gz | wc -l
# With sizes and permissions
tar -tzvf archive.tar.gz | head -20
# Safe habit: extract into a fresh empty directory
mkdir -p ./unpack && tar -xzf archive.tar.gz -C ./unpackHow do I create a .tar.gz archive?
Swap -x for -c. The order of arguments matters: the archive name comes immediately after -f, then the paths you want to include.
# Archive a directory
tar -czf site-backup.tar.gz /var/www/example.com
# Without the leading path, so it unpacks relative rather than absolute
tar -czf site-backup.tar.gz -C /var/www example.com
# Skip the things you never want in a backup
tar -czf site-backup.tar.gz \
--exclude='*.log' \
--exclude='wp-content/cache' \
--exclude='node_modules' \
-C /var/www example.com
# Files and database in one archive
tar -czf full-backup.tar.gz -C /var/www example.com -C /var/backups db.sql-C /var/www example.com form is the one to learn: it stores relative paths, so the archive unpacks anywhere.Excluding cache directories and node_modules routinely cuts a site archive by half or more, and none of it is worth restoring. If you are designing a proper backup routine rather than a one-off, read our backup strategy guide first.
How do I extract a large archive without filling the disk?
The trick is never to write the uncompressed file at all. Pipe the decompressed stream straight into whatever consumes it. For a database dump that means feeding mysql directly, which needs no temporary space beyond the database's own.
# Restore a compressed dump without ever writing the .sql to disk
gunzip -c backup.sql.gz | mysql -u user -p dbname
# Same idea over the network, straight from another server
ssh user@old-server 'gzip -c /var/backups/db.sql' | gunzip -c | mysql -u user -p dbname
# Unpack a tarball straight from a download
curl -sL https://example.com/release.tar.gz | tar -xz -C /var/www/releaseA full disk is an outage, not an inconvenience
When the filesystem fills, MySQL stops accepting writes, PHP cannot create session files and the web server cannot write logs. The site goes down and often stays down after you free the space, because the database needs restarting. Check free space before every extraction, not after.
Knowing how much room you have, and where it went, is the prerequisite. How to check CPU, memory and disk usage covers df, du and finding the directory that grew overnight.
How do I fix common gz and tar errors?
| Error | Cause | Fix |
|---|---|---|
| gzip: stdin: not in gzip format | The file is not gzip, whatever the name says | Run file archive.tar.gz to see what it actually is |
| tar: Error is not recoverable | Truncated or corrupted download | Compare checksums and download again |
| gzip: unexpected end of file | Transfer stopped part-way | Re-fetch; curl -C - resumes a partial download |
| Cannot open: Permission denied | No write permission in the target directory | Extract somewhere you own, or use sudo deliberately |
| No space left on device | Disk filled mid-extraction | Free space, then stream instead of extracting |
| tar: Removing leading '/' | Not an error, just a warning | The archive stored absolute paths; tar is making them relative |
The first row catches most of them. A .tar.gz that is really a zip file is remarkably common.
# What is this file really?
file backup.tar.gz
# Test gzip integrity without extracting
gzip -t backup.tar.gz && echo 'archive is intact'
# Test a tarball end to end (reads it all, writes nothing)
tar -tzf backup.tar.gz > /dev/null && echo 'tarball is readable'
# Compare against a published checksum
sha256sum backup.tar.gzgzip -t and the tar -tzf test are worth running on any backup you are relying on, before the day you need it.A backup you have never tested restoring is a guess. On G7Cloud every nightly backup is restored into a scratch environment and checked automatically, so a corrupt archive is found that night rather than during an incident. See how our backups work.
Frequently asked questions
How do I unzip a .gz file on Linux?
Run gunzip file.gz. It decompresses in place and removes the .gz extension, deleting the compressed original. Use gzip -dk file.gz if you want to keep the compressed file as well, or gunzip -c file.gz > /path/out to write the result somewhere specific.
What does tar -xzf stand for?
x extracts, z decompresses with gzip, and f says the next argument is the filename. Add v for verbose to list each file as it is extracted, giving the commonly seen tar -xzvf.
Does extracting a .gz file delete the original?
Yes, by default. gunzip replaces file.gz with file. That surprises people the first time. Use gzip -dk (decompress, keep) or gunzip -c file.gz > output to leave the compressed file in place.
How do I extract a single file from a tar.gz archive?
Give tar the exact path as it appears inside the archive: tar -xzf archive.tar.gz path/inside/file.txt. Run tar -tzf archive.tar.gz | grep filename first to find the exact path, because it must match precisely.
How much disk space do I need to extract a .tar.gz?
Plan for three to four times the compressed size for text-heavy content such as SQL dumps and source code, which compress well. Archives of images and video compress very little, so the extracted size will be close to the archive size. gzip -l reports the exact figure.
Can I extract a .tar.gz file straight from a URL?
Yes: curl -sL https://example.com/file.tar.gz | tar -xz -C /target/dir. This streams the download directly into tar, so the archive is never written to disk. Only do this with sources you trust, since you are extracting content you have not inspected.
About G7Cloud Engineering
Articles written by the engineers who build and run G7Cloud: UK managed hosting and the AI Website Builder. We write about what we operate every day: containers, backups, databases, and the small-business websites that run on them.
More about G7Cloud →