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

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:
tar -xf archive.tar.gzThe 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.
tar -xvf archive.tar.gzBy 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:
tar -xf archive.tar.gz -C /home/linuxize/filesOn 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:
tar -xf archive.tar.gz file1 file2When 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:
tar -xf archive.tar.gz dir1 dir2If you attempt to extract a file that does not exist, you will see an error message similar to the one below:
tar -xf archive.tar.gz READMEtar: README: Not found in archive
tar: Exiting with failure status due to previous errorsYou 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:
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:
wget -c https://nodejs.org/dist/v22.15.0/node-v22.15.0.tar.gz -O - | tar -xzIf you do not specify a decompression option, tar will indicate which option you should use:
tar: Archive is compressed. Use -z option
tar: Error is not recoverable: exiting nowListing a tar.gz file
To list the content of a tar.gz file, invoke the tar command with the --list (-t) option:
tar -tf archive.tar.gzThe output will look something like this:
file1
file2
file3If you add the --verbose (-v) option, tar will print additional information such as file permissions, ownership, size, and timestamp.
tar -tvf archive.tar.gz-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 file3Extracting 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:
tar -xpf archive.tar.gzThis 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:
tar -xf archive.tar.gz --skip-old-filesTo overwrite files only if they are older than the archive contents:
tar -xf archive.tar.gz --keep-newer-filesQuick Reference
For a printable quick reference, see the tar cheatsheet .
| Task | Command |
|---|---|
| Extract tar.gz | tar -xf archive.tar.gz |
| Extract with verbose | tar -xvf archive.tar.gz |
| Extract to directory | tar -xf archive.tar.gz -C /path/to/dir |
| Extract specific files | tar -xf archive.tar.gz file1 file2 |
| Extract with wildcard | tar -xf archive.tar.gz --wildcards '*.js' |
| List contents | tar -tf archive.tar.gz |
| List with details | tar -tvf archive.tar.gz |
| Preserve permissions | tar -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 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