☰

Main Menu

✕

How to Extract a TAR.GZ File in Linux: Commands & Examples

Many files are compressed and stored using the .tar.gz format — from videos to entire software packages. As software becomes more powerful and media becomes more data-rich, file sizes continue to grow exponentially, and storing and sending files in compressed formats like .tar.gz is increasingly common. Although extracting a .tar.gz file is not as easy as unzipping a .zip, it is still quite simple.

On Linux, you extract a .tar.gz file with tar, not unzip — unzip only handles ZIP archives, despite all the searches for "unzip tar.gz." GNU tar detects gzip compression on a local file by itself, so you only need -z when the archive comes in through a pipe (stdin). This guide walks through extraction methods, useful options, common errors, and security tips.

📄 Need a quick reference? Check out our comprehensive Linux Commands Cheat Sheet for all the essentials.

⚡ tar.gz Quick Start

tar -xf archive.tar.gz                    # extract here
tar -xvf archive.tar.gz                   # extract and list each file
tar -xf archive.tar.gz -C /path/to/folder # extract somewhere else
Stylised terminal illustration showing tar -xvf archive.tar.gz and extracted file paths line by line

❓ What Is a tar.gz File?

A “.tar” file is a collection of files bundled into a single archive. Short for Tape ARchive, the name is a throwback to when files were stored on magnetic tape drives. Much like .zip files, .tar files package large files into a single unit, making them easier to store and send. A tar.gz file — or tarball — is a tar archive of files and folders that gzip has then compressed. These files are commonly used by Linux, macOS, and Windows users for backups or data archival.

Difference between .tar, .gz, .tar.gz, and .tgz

Extension What it is Extract with
.tar Uncompressed archive container tar -xf file.tar
.gz One gzip-compressed file gunzip file.gz or gzip -d file.gz
.tar.gz tar archive compressed with gzip tar -xf file.tar.gz
.tgz Short extension for the same format as .tar.gz tar -xf file.tgz

Why source code and Linux software use tar.gz

tar stores Unix permissions, ownership, and symlinks, and every Linux system ships with tar and gzip. That's why source releases and server backups usually come as tarballs.

🗜️ Advantages of Using .tar.gz Files

Compressed formats like .tar.gz offer several advantages that make them a popular choice for archiving and transfer.

  1. Efficient Compression: GZIP is known for excellent compression, reducing the size of files and directories for easier storage and transfer.
  2. File Integrity: The format includes checksums that verify the archived data — no corruption during storage or transfer.
  3. Cross-Platform Compatibility: Works across multiple operating systems — Linux, Windows, macOS, and other Unix-like systems.
  4. Preserves File Attributes: Keeps permissions, ownership, and timestamps — crucial when archiving files with specific metadata.
  5. File Grouping: Bundles multiple files and directories into a single package for easier organization and backup.
  6. Incremental Backups: You can update an existing archive by adding or replacing only changed files.

🔍 .tar vs .tar.gz: What's the Difference?

.tar files are collections of uncompressed files. To compress them, you pair them with gzip, which produces .tar.gz files. .zip files are pre-compressed file-by-file — no extra steps required. Choosing between them is a trade-off: .zip is easier to access but less space-efficient, while .tar.gz saves more space. Other compression techniques include .tar.bz2, .tar.br, and .tar.zst.

After mastering the Zip Command in Linux, you may want to reverse the process — check out our detailed guide on Linux Unzip to easily access your files.

📌 Important Symbols in tar.gz Command

  • x: Extract files.
  • v: Verbose — lists files as they're processed.
  • z: Uncompress (gzip).
  • f: Specify the filename.

🐧 How to Extract a tar.gz File in Linux

Most Linux distributions include built-in tools to compress and decompress .tar.gz files. The tar utility is sufficient for extraction; you need gzip only to create .tar.gz files.

Extract in the current directory

tar -xf archive.tar.gz

The files land in your current working directory, so cd into the right folder first.

Show files while extracting with -v

tar -xvf archive.tar.gz

Verbose mode prints each file as tar writes it — on a large archive, it tells you it hasn't stalled.

Extract to another directory with -C

mkdir -p /home/user/downloads/app
tar -xf archive.tar.gz -C /home/user/downloads/app

tar won't create the target directory, so run mkdir -p first.

📋 How to List the Contents of a tar.gz Archive

tar -tf archive.tar.gz    # file names only
tar -tvf archive.tar.gz   # permissions, owner, size, date

A listing shows the exact stored paths and whether everything sits in one top folder or will spill into your current directory.

Stylised terminal illustration showing tar -tvf archive.tar.gz with metadata columns and archive paths.

🎯 How to Extract Only Specific Files or Folders

tar -xf archive.tar.gz project/config/app.conf   # one file
tar -xf archive.tar.gz project/docs               # one directory
tar -xf archive.tar.gz --wildcards '*.conf'       # pattern match

The name has to match the stored path exactly, leading folder included. If tar says "Not found in archive," copy the path from tar -tf.

📥 How to Extract tar.gz from stdin or a URL

You can stream a download straight into tar without saving it:

wget -qO- https://example.com/app.tar.gz | tar -xzf -
curl -sL https://example.com/app.tar.gz | tar -xzf -

-f - tells tar to read from standard input. tar can't inspect a pipe the way it inspects a file, so add -z yourself. The -L flag makes curl follow redirects, which most download links use. To extract to a specific directory:

wget -qO- https://example.com/archive.tar.gz | tar -xzf - -C /path/to/destination

⚙️ Useful tar Options Explained

Option What it does
-x Extract files
-f Names the archive file (- means stdin)
-v Verbose output
-t List contents
-C Change to a directory before extracting
-z Filter through gzip
-p Preserve the archived permissions

--strip-components

tar -xf app.tar.gz --strip-components=1 drops the first folder level, so app-1.2/bin/run becomes bin/run.

--one-top-level

tar -xf archive.tar.gz --one-top-level puts everything in a folder named after the archive — saving you from archives that dump dozens of files into your current directory.

--keep-old-files and --skip-old-files

By default tar overwrites existing files. --keep-old-files reports an error instead, and --skip-old-files skips them silently.

-p for permissions

As root, tar restores the archived permissions by default. Regular users get those permissions minus their umask unless they add -p. chmod can fix modes afterward.

🆕 How to Create a tar.gz Archive in Linux

tar -czf archive.tar.gz folder/   # create from a folder
gzip backup.tar                   # compress an existing .tar into backup.tar.gz

-c creates the archive and -z compresses it. Unlike extraction, creation won't compress unless you ask. To decompress a .tar.gz with gzip directly, use gzip -d filename.tar.gz. Keep the original with gzip -k filename.tar or output to a different file with gzip -c filename.tar.

🚨 Common Errors and How to Fix Them

tar: Archive is compressed. Use -z option

You probably piped gzip data into tar without -z. Use tar -xzf -.

gzip: stdin: not in gzip format

The file isn't gzip. It's often an HTML error page saved as .tar.gz, or a renamed plain .tar. Run file archive.tar.gz to check, then drop -z or download it again.

Target directory does not exist

-C with a missing folder gives "Cannot open: No such file or directory." Create it with mkdir -p first.

File not found in archive

The path doesn't match what's stored. List the contents with tar -tf and copy the exact path.

Stylised terminal graphic showing tar pipe error without -z and success with tar -xzf -.

🛡️ Security Tips Before Extracting Archives

Treat archives from unknown sources like any untrusted download.

  1. List the contents first with tar -tvf.
  2. Extract into an empty scratch directory such as /tmp/archive-test, never straight into / or your home folder.
  3. Watch for absolute paths, ../ entries, symlinks pointing outside the folder, and odd hidden files.
  4. Don't extract untrusted archives as root.

GNU tar strips leading slashes and refuses member names containing .. by default, but symlink tricks can still cause trouble. The GNU tar manual has the details.

💻 How to Open tar.gz Files on Windows and macOS

Windows built-in tar

Windows 10 and 11 include a built-in tar command. It's bsdtar and accepts the same tar -xf archive.tar.gz syntax in Command Prompt or PowerShell. Since a late 2023 update, Windows 11 File Explorer can also open .tar and .gz archives directly.

When 7-Zip makes sense

You don't need 7-Zip for tar.gz. It helps on older Windows builds or if you want one GUI tool for every format.

macOS Finder and Terminal

macOS ships with built-in tar and gzip utilities. Double-click the file in Finder, or run tar -xf in Terminal. On Linux desktops, right-click the file and choose "Extract Here."

🆚 tar.gz vs ZIP: Which Should You Use?

For anything that stays on Linux — backups, deployments, source code — use tar.gz, because it keeps permissions and symlinks. ZIP suits Windows users better, and since each file inside a ZIP is compressed on its own, you can pull single files out faster.

📋 tar.gz Command Cheat Sheet

Task Command
Extract tar -xf file.tar.gz
Extract to a folder tar -xf file.tar.gz -C /path
List contents tar -tf file.tar.gz
Extract one file tar -xf file.tar.gz path/to/file
Extract from URL curl -sL URL | tar -xzf -
Drop top folder tar -xf file.tar.gz --strip-components=1
Create tar -czf file.tar.gz folder/

🧩 Conclusion

tar.gz files — tarballs — archive data and backups, and knowing how to decompress and extract them is essential for day-to-day Linux work. You now know the key extraction methods, the options that matter, how to create archives, the errors you'll hit, and how to inspect untrusted archives safely. To put these commands to work on a reliable, high-performance platform, explore Linux VPS hosting from MonoVM — dedicated resources and full root access for your projects.

Category: Linux

Write Comment