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.
In Linux, you can extract .tar.gz files in two ways:
ls and moving between directoriesmacOS and Windows can also extract .tar.gz files, but the instructions below cover Linux only.
A .tar.gz file has two parts: a tar archive and gzip compression.
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 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.
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:
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 extractTip: current GNU tar detects gzip compression automatically when you extract, so tar -xvf file.tar.gz works too.
Extracting to a different directory can:
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.
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.
You can check what's inside a .tar.gz file before you extract it.
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.
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'
You don't have to extract everything at once. You can pull out individual files, folders or file types.
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.
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'
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.
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:
Follow these steps to extract a file with one of these tools:
Menu names vary by Linux desktop environment.
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
Three errors come up often when you extract .tar.gz files from the command line.
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.
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.
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
Two related commands are useful when you work with .tar.gz files.
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.
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
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.