How to Extract (Unzip) tar.gz Files in Linux

By 

Updated on

8 min read

Extracting tar.gz files in Linux with the tar command

Files that end in .tar.gz (or .tgz) are tar archives compressed with gzip . You will run into them when downloading source code, backups, or open-source packages on Linux and other Unix-like systems.

A tar archive (short for “Tape Archive”) bundles multiple files and directories into a single file. Gzip then compresses that archive to save space. The tar command handles both the archiving and the decompression in one step.

This guide explains how to extract tar.gz files in Linux, including extracting to a specific directory, pulling individual files, using wildcards, and listing archive contents before unpacking.

Quick Reference

For a printable quick reference, see the tar cheatsheet .

TaskCommand
Extract tar.gztar -xf archive.tar.gz
Extract .tgztar -xf archive.tgz
Extract with verbose outputtar -xvf archive.tar.gz
Extract to another directorytar -xf archive.tar.gz -C /path/to/dir
Extract and remove the top-level directorytar -xf archive.tar.gz --strip-components=1 -C /path/to/dir
Extract specific filestar -xf archive.tar.gz file1 file2
Extract matching filestar -xf archive.tar.gz --wildcards '*.js'
List contentstar -tf archive.tar.gz
List contents with detailstar -tvf archive.tar.gz
Extract from stdinwget -O - URL | tar -xzf -
Preserve permissionstar -xpf archive.tar.gz
Skip existing filestar -xf archive.tar.gz --skip-old-files

Extracting tar.gz File

The tar command is pre-installed by default on most Linux distributions and macOS.

Although you may search for “unzip tar.gz”, .tar.gz files are normally extracted with tar, not with the unzip command. To extract a local tar.gz file, use the --extract (-x) option and specify the archive file name after the -f option:

Terminal
tar -xf archive.tar.gz

The tar command auto-detects the compression type for local archives. You can use the same command to extract tar archives compressed with other algorithms such as .tar.bz2 .

A .tgz file is the same format as .tar.gz, so the command is identical:

Terminal
tar -xf archive.tgz

The -v option makes the tar command more verbose and prints the names of the files being extracted on the terminal.

Terminal
tar -xvf archive.tar.gz

By default, tar extracts the archive contents in the current working directory . If the archive contains a top-level directory, tar recreates that directory and places the extracted files inside it.

On a Linux desktop, you can also right-click a tar.gz file in the file manager and select “Extract Here” or “Extract to…” instead of using the terminal.

Extracting to Another Directory

Use the --directory (-C) option to specify where you want to unpack the archive. The target directory must already exist.

For instance, to extract the archive contents to the /home/linuxize/files directory, you can use the following command:

Terminal
tar -xf archive.tar.gz -C /home/linuxize/files

If the directory does not exist, create it first:

Terminal
mkdir -p /home/linuxize/files
tar -xf archive.tar.gz -C /home/linuxize/files

Many source archives extract into a top-level directory such as project-1.2.3/. If you want the files directly inside the target directory, use --strip-components=1 to remove the first path component during extraction:

Terminal
mkdir -p /opt/project
tar -xf project-1.2.3.tar.gz --strip-components=1 -C /opt/project

List the archive contents first before using --strip-components, so you know which path component will be removed.

Listing a tar.gz File

Before extracting an archive from an unfamiliar source, list its contents. This shows the paths that tar will write.

To list the content of a tar.gz file, invoke the tar command with the --list (-t) option:

Terminal
tar -tf archive.tar.gz

The output will look something like this:

output
file1
file2
file3

If you add the --verbose (-v) option, tar will print additional information such as file permissions, ownership, size, and timestamp.

Terminal
tar -tvf archive.tar.gz
output
-rw-r--r-- linuxize/users       0 2026-01-15 01:19 file1
-rw-r--r-- linuxize/users       0 2026-01-15 01:19 file2
-rw-r--r-- linuxize/users       0 2026-01-15 01:19 file3

Use this list when you need the exact path for extracting one file or directory.

Extracting Specific Files from a tar.gz File

To extract specific files from a tar.gz archive, include a space-separated list of the file names after the archive name:

Terminal
tar -xf archive.tar.gz file1 file2

When extracting files, you must provide their exact names along with the path, as listed by tar --list (tar -t).

To extract one or more directories from an archive, you can follow the same process as extracting individual files:

Terminal
tar -xf archive.tar.gz dir1 dir2

If you attempt to extract a file that does not exist, you will see an error message similar to the one below:

Terminal
tar -xf archive.tar.gz README
output
tar: README: Not found in archive
tar: Exiting with failure status due to previous errors

You can also extract files from a tar.gz file based on a wildcard pattern, by using the --wildcards option and quoting the pattern to prevent the shell from interpreting it.

For example, to extract files whose names end in .js (JavaScript files), you would use:

Terminal
tar -xf archive.tar.gz --wildcards '*.js'

Extracting tar.gz File from stdin

If you are extracting a compressed tar.gz file by reading the archive from stdin (usually through a pipe), you need to specify the decompression option. The option that tells tar to read the archive through gzip is -z.

In the following example we are downloading the Node.js sources using the wget command and piping its output to the tar command:

Terminal
wget -c https://nodejs.org/dist/v22.15.0/node-v22.15.0.tar.gz -O - | tar -xzf -

If you do not specify a decompression option, tar will indicate which option you should use:

output
tar: Archive is compressed. Use -z option
tar: Error is not recoverable: exiting now

Extracting and Preserving Permissions

When run as root, tar restores the stored permissions by default. As a regular user, extracted files get your umask applied, so the modes may differ from what the archive stores. Use the -p option to restore the exact permission bits from the archive:

Terminal
tar -xpf archive.tar.gz

Ownership restoration depends on the user running the command and the ownership metadata in the archive. A regular user cannot change extracted files to another owner.

Handling Existing Files

When extracting, tar will overwrite existing files by default. To skip files that already exist, use the --skip-old-files option:

Terminal
tar -xf archive.tar.gz --skip-old-files

To overwrite files only if they are older than the archive contents:

Terminal
tar -xf archive.tar.gz --keep-newer-files

If you want tar to fail instead of overwriting an existing file, use --keep-old-files:

Terminal
tar -xf archive.tar.gz --keep-old-files

Troubleshooting

tar: archive.tar.gz: Cannot open: No such file or directory
The archive name or path is wrong, or you are running the command from a different directory. Check the file name with ls, or provide the full path:

Terminal
tar -xf /path/to/archive.tar.gz

tar: README: Not found in archive
The file path you provided does not match the path stored in the archive. List the archive contents first:

Terminal
tar -tf archive.tar.gz

Then copy the exact path from the output and use it in the extract command.

tar: Archive is compressed. Use -z option
This usually happens when you read a tar.gz archive from stdin without telling tar which decompressor to use. Add -z and use - as the archive name:

Terminal
cat archive.tar.gz | tar -xzf -

For local files, prefer tar -xf archive.tar.gz, because tar can usually detect gzip compression from the file itself.

gzip: stdin: not in gzip format
The file may not be gzip-compressed, even if its name ends in .tar.gz. Check the file type:

Terminal
file archive.tar.gz

If the file is a plain .tar archive, extract it with tar -xf archive.tar. If it is a ZIP file, use unzip instead.

The target directory does not exist
The -C option does not create directories. Create the directory first, then extract:

Terminal
mkdir -p /path/to/dir
tar -xf archive.tar.gz -C /path/to/dir

You do not want to overwrite existing files
By default, tar overwrites files with matching paths. Use --keep-old-files to stop on the first existing file, or --skip-old-files to skip existing files and continue.

FAQ

What is the difference between tar.gz and zip?
Both are compressed archives, but they work differently. A .zip file compresses each file individually, which allows you to extract a single file without decompressing the rest. A .tar.gz file first bundles all files into one tar archive, then compresses the whole thing with gzip. Tar.gz typically achieves better compression ratios and is the standard format on Linux and Unix systems.

Can I extract a tar.gz file without installing anything?
Yes. The tar command is pre-installed on virtually all Linux distributions and macOS. No additional software is needed.

How do I extract a .tgz file?
A .tgz file is identical to a .tar.gz file. Use the same command: tar -xf archive.tgz.

How do I see what is inside a tar.gz file before extracting?
Use tar -tf archive.tar.gz to list the contents without extracting. Add -v for detailed output including permissions and file sizes.

Conclusion

The tar -xf archive.tar.gz command is the simplest way to extract .tar.gz and .tgz files from disk. For related archive formats, see the guides for tar.bz2 and tar.xz files, or learn how to create tar.gz files .

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page