How-to

Extract tar.gz Files from the Command Line

Most software, backups and data sets arrive as .tar.gz. One command unpacks them; a few options decide where the files land and who owns them.

What is the basic command?

tar -xzf backup-web.tar.gz

Extract (-x), gunzip (-z), from file (-f). The files land in the current folder with the paths stored in the archive. Current GNU tar detects gzip by itself, so tar -xf is enough, and the .tgz extension is the same format. Add -v to print each file name as it is written.

How do I choose where files go?

Use -C with a folder that already exists:

mkdir -p /srv/restore/web
tar -xzf backup-web.tar.gz -C /srv/restore/web

Extracting a restore into a separate folder, then comparing or moving files into place, is safer than unpacking straight over a live site. A wrong archive cannot overwrite production that way.

How do I see what is inside without extracting?

tar -tzf backup-web.tar.gz | less
tar -tvzf backup-web.tar.gz | sort -k3 -n | tail -5

The first command lists paths. The second shows details and sorts by size, which finds the biggest files quickly. Always list unknown archives first: if paths start with / or contain ../, be careful. GNU tar strips leading slashes and refuses paths that climb out of the target by default, but it is good practice to look anyway.

How do I extract only some files?

Give the exact paths as they appear in the listing, or wildcards with --wildcards:

tar -xzf backup-web.tar.gz srv/site/.env srv/site/config/app.php
tar -xzf backup-web.tar.gz --wildcards 'srv/site/storage/logs/*.log' -C /tmp/logs

Paths must match exactly, including any leading ./ that the archive stored. If tar says "Not found in archive", copy the path from the listing.

What does --strip-components do?

It removes leading path parts while extracting. Release tarballs usually wrap everything in a versioned folder such as tool-1.8.2/. To install straight into /opt/tool:

mkdir -p /opt/tool
tar -xzf tool-1.8.2-linux-amd64.tar.gz -C /opt/tool --strip-components=1

On Arm models such as A1, download the arm64 or aarch64 build instead of amd64; a binary for the wrong architecture extracts fine but fails with "Exec format error" when you run it.

Who owns the extracted files?

That depends on who runs tar. As a normal user, files belong to you, whatever the archive says. As root, GNU tar restores the stored owners and modes, which is right for backups but can surprise you with release tarballs built on someone's laptop under an odd user ID.

OptionEffect when run as root
defaultRestores stored owner and group
--no-same-ownerFiles belong to root
--numeric-ownerUses stored numeric IDs, ignoring names
-p / --same-permissionsKeeps stored modes exactly (default for root)

For software installs, --no-same-owner followed by a deliberate chown is cleanest. For full system restores between machines, --numeric-owner avoids mapping IDs through user names that may differ.

How do I extract from a download without saving it?

Stream it into tar:

curl -fsSL https://downloads.example.org/tool-1.8.2.tar.gz | tar -xz -C /opt/tool --strip-components=1

-f in curl makes it fail on HTTP errors instead of piping an error page into tar. When a checksum is published, download to a file first and verify it with sha256sum -c before extracting; streaming skips that check.

What do common errors mean?

  • "gzip: stdin: not in gzip format": the file is not gzip. Check it with file; it may be plain tar, xz, zstd or an HTML page.
  • "Unexpected EOF in archive": the file is truncated. Download it again.
  • "Cannot change ownership": you extracted as a normal user into a place where tar tried to restore owners, which is harmless; add --no-same-owner to silence it.
  • "No space left on device": check free space with df -h and the uncompressed size with gzip -l, which works for files under 4 GB.

Creating archives is covered in Create tar.gz archives, and every tar option worth knowing in The tar command.

Keep Reading

Create tar.gz archives

Pack folders into tar.gz archives, choose compression levels, use pigz on many cores and compare gzip with zstd and xz.

Open

Extract .tar.bz2 files

Open .tar.bz2 and .tbz2 archives, list them first, extract single files, and speed up big ones with parallel bzip2 on many cores.

Open

The tar command

A working reference for tar: modes, compression flags, appending, comparing, incremental backups and the options people forget.

Open

Arm vs x86 servers

How Ampere Arm processors differ from Intel and AMD x86 chips, and which jobs run cheaper on each.

Open

FAQ

Quick Answers

Run tar -xzf followed by the file name, or tar -xf since tar detects gzip automatically.

Create the folder and add -C with its path to the tar command.

Use --strip-components=1 to drop the first path element of every file.

The file is another format or not an archive at all. Check it with the file command.

Pick builds labeled arm64 or aarch64; amd64 builds will not run on the Arm models.