Linux7 min readPublished: 18 Jul 2024Updated: 29 Jul 2026

How to Extract .gz and .tar.gz Files on Linux

The difference between .gz and .tar.gz, the commands to extract each one, how to keep the original, and how to unpack a huge archive without filling the disk.

G7Cloud Engineering
Platform team
Share:
Abstract illustration representing how to extract .gz and .tar.gz files on linux.
  • .gz is one compressed file. .tar.gz is many files bundled together and then compressed. They need different commands.
  • Extract a .gz with gunzip file.gz, which replaces the original. Use gzip -dk file.gz to keep it.
  • Extract a .tar.gz with tar -xzf archive.tar.gz, and add -C /target/dir to 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.

ExtensionWhat it isExtract with
.gzA single compressed filegunzip file.gz
.tarMany files bundled, not compressedtar -xf archive.tar
.tar.gz or .tgzMany files bundled, then compressedtar -xzf archive.tar.gz
.sql.gzA compressed database dump (a single file)gunzip dump.sql.gz
.tar.bz2Bundled, compressed with bzip2tar -xjf archive.tar.bz2
.tar.xzBundled, compressed with xztar -xJf archive.tar.xz

The letter in the tar flag names the compressor: z for gzip, j for bzip2, J for xz.

Pro Tip

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.

Extracting a single .gz file
# 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.gz
zcat, zless and zgrep read compressed files directly. On a low-disk server they are often the only workable option.

Check 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.

How big will it actually be?
# 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.

The commands you will actually use
# 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.

Inspecting an archive first
# 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 ./unpack
The last line is the habit worth building. An empty target directory means an untidy archive can never overwrite anything.

How 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.

Creating archives
# 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
The -C /var/www example.com form is the one to learn: it stores relative paths, so the archive unpacks anywhere.
Pro Tip

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.

Streaming, no temporary file
# 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/release
Each of these avoids a temporary file entirely, which is often the difference between a restore that works and one that fills the disk halfway through.

A 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?

ErrorCauseFix
gzip: stdin: not in gzip formatThe file is not gzip, whatever the name saysRun file archive.tar.gz to see what it actually is
tar: Error is not recoverableTruncated or corrupted downloadCompare checksums and download again
gzip: unexpected end of fileTransfer stopped part-wayRe-fetch; curl -C - resumes a partial download
Cannot open: Permission deniedNo write permission in the target directoryExtract somewhere you own, or use sudo deliberately
No space left on deviceDisk filled mid-extractionFree space, then stream instead of extracting
tar: Removing leading '/'Not an error, just a warningThe 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.

Verify before you trust an archive
# 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.gz
gzip -t and the tar -tzf test are worth running on any backup you are relying on, before the day you need it.
Pro Tip

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.

Put this into practice on G7Cloud

Every site runs in its own dedicated container behind ScaleShield, with daily backups that are restore-tested every night. Start on the free plan, no card needed.

About G7Cloud Engineering

Platform team

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 →