Linux7 min readPublished: 18 Jul 2024Updated: 9 Nov 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 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.

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

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 server short of disk space they are often the only option.

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

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

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

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
Make the last line a habit. With an empty target directory, an untidy archive cannot overwrite anything.

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

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
Learn the -C /var/www example.com form. It stores relative paths, so the archive unpacks anywhere.
Pro Tip

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.

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
None of these writes a temporary file, which is often the difference between a restore that works and one that fills the disk halfway through.

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

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 on purpose
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 surprisingly 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
Run gzip -t and the tar -tzf test on any backup you rely on, before the day you need it.
Pro Tip

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.

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 →