.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 choose where it lands. - List the contents first with
tar -tzf: an archive without a top-level folder scatters files across your current directory. - Before extracting a compressed database dump, make sure you have three to four times the archive's size in free disk space.
What is the difference between .gz and .tar.gz?
Run gunzip file.gz for a .gz file and tar -xzf archive.tar.gz for a .tar.gz. They differ because gzip compresses exactly one file, while tar bundles many files into one archive (a tarball) without compressing them. A .tar.gz is both: bundled by tar, then compressed by gzip.
| 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 itself, so tar -xf archive.tar.gz works without the z. Type the letter anyway. It says what the file is, and it still matters on older systems and in scripts that may run anywhere.
How do I extract a .gz file?
gunzip file.gz decompresses the file in place and removes the .gz. That means the compressed original is gone once it is unpacked. To keep it, add 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. Extract it on a server with 10 GB free and the disk fills, which 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 at the end of the file, 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 mean extract, gzip and file. Unlike gunzip, tar leaves the archive in place.
# 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 moves into the target directory before extracting, so the files land there, wherever you are when you run it.List before you extract
Some archives hold a single top-level folder and unpack tidily. Others hold hundreds of loose files that land straight in your current directory, mixed in with whatever was already there. tar -tzf archive.tar.gz | head tells you which kind you have in 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 (create). The order matters: the archive name comes straight 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. It stores relative paths, so the archive unpacks anywhere.Leaving out cache folders and node_modules often halves a site archive, and none of it is worth restoring. If you are planning a backup routine rather than a one-off, read our backup strategy guide first.
How do I extract a large archive without filling the disk?
Never write the uncompressed file at all. Pipe the decompressed data straight into whatever uses it. For a database dump, that means feeding it to mysql directly, which needs no 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
When the disk fills, MySQL stops accepting writes, PHP cannot save 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.
Knowing how much room you have, and where it went, comes first. How to check CPU, memory and disk usage covers df, du and finding the folder 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 on purpose |
| 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 surprisingly 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 on any backup you rely on, before the day you need it.A backup you have never restored is a guess. On G7Cloud, backups are checked for corruption every night, and a rotating sample is restored into a throwaway test environment to prove they work. 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 to keep the compressed file as well, or gunzip -c file.gz > /path/out to write the result somewhere else.
What does tar -xzf stand for?
x extracts, z decompresses with gzip, and f says the next argument is the file name. Add v (verbose) to list each file as it comes out, which gives the familiar tar -xzvf.
Does extracting a .gz file delete the original?
Yes, by default. gunzip replaces file.gz with file, which 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 path, because it must match exactly.
How much disk space do I need to extract a .tar.gz?
Plan for three to four times the compressed size for text such as SQL dumps and source code, which compress well. Images and video barely compress, 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 straight into tar, so the archive is never written to disk. Only do it with sources you trust, because you are unpacking files 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 →