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

By 

Updated on

5 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.

Extracting tar.gz File

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

To extract a 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 will auto-detect the compression type and extract the archive. You can use the same command to extract tar archives compressed with other algorithms such as .tar.bz2 .

The -v option makes the tar command more visible 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 . Use the --directory (-C) option to specify a directory in which you want to unpack the archive.

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

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 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 pipe its output to the tar command:

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

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

Listing a tar.gz file

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

Extracting and Preserving Permissions

By default, tar preserves file permissions when extracting. If you need to explicitly preserve permissions (useful when running as root), use the -p option:

Terminal
tar -xpf archive.tar.gz

This ensures that the original file permissions, ownership, and timestamps are maintained.

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

Quick Reference

For a printable quick reference, see the tar cheatsheet .

TaskCommand
Extract tar.gztar -xf archive.tar.gz
Extract with verbosetar -xvf archive.tar.gz
Extract to directorytar -xf archive.tar.gz -C /path/to/dir
Extract specific filestar -xf archive.tar.gz file1 file2
Extract with wildcardtar -xf archive.tar.gz --wildcards '*.js'
List contentstar -tf archive.tar.gz
List with detailstar -tvf archive.tar.gz
Preserve permissionstar -xpf archive.tar.gz

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

We covered how to extract tar.gz files including extracting to specific directories, extracting specific files, and listing archive contents.

For more information on creating tar archives, see our guide on creating tar.gz files . You may also want to check out guides for tar.bz2 and tar.xz 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