Delinea Blog > How to extract or unzip .tar.gz files in Linux

How to extract or unzip .tar.gz files in Linux

Published October 2024
Read time 6 minutes
What you will learn
How to extract .tar.gz files in Linux from the command line or a graphical tool, extract only the files you need and fix common errors.

A .tar.gz file is a compressed archive that combines the tar and gzip formats. System administrators, developers and everyday users rely on them to archive and compress files.

You might need to extract one when you transfer large datasets or install software distributed for Linux.

Extraction looks simple, but a careless command can overwrite files you need or let an untrusted archive write where it shouldn't. The steps below cover the commands and the habits that help you avoid those mistakes.

What you need to extract .tar.gz files in Linux

In Linux, you can extract .tar.gz files in two ways:

  • the command line, which takes basic terminal knowledge, such as running  ls  and moving between directories
  • a graphical user interface (GUI)

macOS and Windows can also extract .tar.gz files, but the instructions below cover Linux only.

What is a .tar.gz file?

A .tar.gz file has two parts: a tar archive and gzip compression.

Tar archive

A tar archive bundles multiple files or directories into a single uncompressed archive, often known as a tarball. This makes it easier to distribute software or manage a large number of files. Tar stands for "tape archive."

Gzip compression

Gzip compresses a tar archive to reduce its size, so it transfers faster and takes up less storage. 

When gzip compresses a tar archive, the result is a .tar.gz file.

How to extract a .tar.gz file in Linux

The most common way to extract .tar.gz files is from the command line, also called the terminal or console. This section covers three methods:

  • running the basic command 
  • extracting files to a specific directory
  • extracting multiple files at once

Basic command to extract a .tar.gz file

Open a terminal and run:

tar -xvzf file.tar.gz

This command uses the following flags:

  • -x  extracts files from the archive
  • -v  turns on verbose mode, which lists each file as it's extracted
  • -z  handles gzip compression
  • -f  specifies the archive file to extract

Tip: current GNU tar detects gzip compression automatically when you extract, so  tar -xvf file.tar.gz  works too.

Extract files to a specific directory

Extracting to a different directory can:

  • help keep files organized. 
  • avoid overwriting files in the current directory.
  • put files on a disk with more free space.

To extract files to a new location, use the  -C  option to specify a target directory:

tar -xvzf file.tar.gz -C /path/to/directory

Here's an example:

tar -xvzf archive.tar.gz -C /home/user/Documents/

The target directory must already exist. Create it first with  mkdir -p /path/to/directory. 

Extract multiple .tar.gz files at once

A  for  loop extracts every .tar.gz file in the current directory:

for file in *.tar.gz; do tar -xvzf "$file"; done

If you're not comfortable with the command line, the graphical tools section later in this guide covers the same task. 

How to view the contents of a .tar.gz file

You can check what's inside a .tar.gz file before you extract it.

List everything in the archive

To view the contents of a .tar.gz file, run:

tar -tvzf file.tar.gz

The  -t  flag lists every file in the archive without extracting anything. 

List specific file types with wildcards

To list only certain file types, such as .txt files, use a wildcard. This command shows only the .txt files in the archive:

tar -tvzf file.tar.gz --wildcards '*.txt'

How to extract specific files from a .tar.gz file

You don't have to extract everything at once. You can pull out individual files, folders or file types. 

Extract selected files or folders

To extract a single file or folder from the archive, specify its path:

tar -xvzf file.tar.gz path/to/file

For example, to extract file.txt:

tar -xvzf archive.tar.gz file.txt

The path must match the path stored in the archive exactly, so list the archive first to check it.

Extract specific file types with wildcards

To extract every file of one type, use a wildcard. This command extracts all the .log files in the archive:

tar -xvzf file.tar.gz --wildcards '*.log'

How to extract without overwriting existing files

If the target directory already has files with the same names as files in the archive, tar replaces them by default, and the originals are lost.

To keep existing files, add  --skip-old-files:

tar --skip-old-files -xvzf file.tar.gz

This extracts only files that don't already exist and leaves the existing ones unchanged.

How to download and extract a .tar.gz file in one step

Downloading and extracting in one step saves time, but use it only with archives you trust.

Use  wget to stream the archive and pipe it straight into tar, which reads it from standard input (stdin):

wget -qO- https://example.com/file.tar.gz | tar -xz

The  -qO-  option tells  wget  to run quietly and send the download to the pipe instead of saving it.

Because the compressed file is never saved you can't check its checksum before extracting. For any archive you don't fully trust, download the file, verify its checksum or signature and then extract it. 

Avoid running tar with  sudo  on downloaded archives, because a malicious archive could overwrite system files. Extract into a folder you own instead.

How to extract a .tar.gz file without the command line

You don't have to use the command line to extract .tar.gz files. A GUI lets you extract archives on Linux without typing commands.

Two common GUI tools extract .tar.gz files:

  • File Roller (the GNOME Archive Manager)
  • Xarchiver

Follow these steps to extract a file with one of these tools:

  1. Open your file manager. 
  2. Go to the folder that contains the .tar.gz file. 
  3. Right-click the file and choose Extract Here or Extract To, or open it with File Roller or Xarchiver.
  4. If prompted, choose where to save the extracted files.

Menu names vary by Linux desktop environment.

How to automate .tar.gz extraction with a Bash script

If you extract archives regularly, save the loop from the bulk extraction section as a Bash script. It's useful for repetitive tasks such as restoring backups.

This script extracts every .tar.gz file in the directory where you run it:

#!/bin/bash
for file in *.tar.gz; do
  tar -xvzf "$file"
done

How to fix common .tar.gz extraction issues

Three errors come up often when you extract .tar.gz files from the command line. 

Permission-denied error

A permission-denied error usually means you can't write to the directory you're extracting into. Instead of using sudo, extract into a folder you own:

mkdir -p ~/extracted

tar -xvzf archive.tar.gz -C ~/extracted

Use  sudo only when you need to write a system directory. When tar runs as root, it restores the file owners stored in the archive. Add  --no-same-owner  so the extracted files belong to the user running tar instead. 

File-not-found error

Check that the file path is correct. Run  ls  to list the files in the directory and confirm the filename. Press Tab to autocomplete file paths and avoid typos.

Corrupted archive

A checksum tells you whether a download is damaged or has been changed. It doesn't repair the file. Compare the output of  sha256sum archive.tar.gz  with the value the publisher lists. If they don't match, download the file again. 

You can also test the archive without extracting it:  gzip -t archive.tar.gz

Other tar and gzip commands to know

Two related commands are useful when you work with .tar.gz files. 

Create a .tar.gz archive

To create a .tar.gz archive, run:

tar -cvzf archive.tar.gz /path/to/folder

The  -c  flag creates a new archive, bundling files and folders into one compressed file. For more options, see how to tar a file in Linux.

Use gzip on its own

Gzip by itself can help you compress or decompress individual files that don't need the full tar utility. To decompress a file:

gzip -d file.tar.gz

This removes the original .gz file and leaves file.tar. To keep the original, add  -k:

gzip -dk file.tar.gz

Extract archives with least privilege in mind

Extracting .tar.gz files in Linux lets you handle large datasets and install software more effectively. Most of the steps above use the command line, and graphical tools cover the same task if you prefer them.

Whichever method you use, the same habits help keep extraction safe: verify checksums for downloaded archives, extract into a folder you own and save  sudo  for the cases that need it. That's least privilege in practice, and on servers it applies to every administrative task.

See how Privilege Control for Servers enforces least privilege on Linux servers.

Related Topics