How To Extract (Unzip) Tar Gz File - Linuxize

If you are using open source software, chances are you’ll encounter .tar.gz files on a regular basis.

Open-source packages are often distributed in .tar.gz and .zip formats.

Tar archives, often referred to as tarballs, are created by converting a group of files into an archive. These archives are mostly used for backup or software distribution purposes. In Linux and Unix-like systems, you can create tar archives using the tar command. It supports a vast range of compression programs such as gzip, bzip2, lzip, lzma, lzop, xz, and compress. Tar was originally designed for creating archives to store files on magnetic tape, which is why it is named “Tape ARchive”.

Gzip is the most popular algorithm for compressing tar files. By convention, the name of a tar archive compressed with gzip should end with either .tar.gz or .tgz.

In short, a file that ends in .tar.gz is a .tar archive compressed with gzip.

Besides creating new archives, the tar command can also be used for various other operations, including extracting tar archives, displaying a list of files included in the archive, and adding additional files to an existing archive.

This guide covers how to extract (or unzip) tar.gz and tgz archives.

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:

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

Terminaltar -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:

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

If you’re a desktop user and prefer not to use the command-line interface, you can use your file manager instead. To extract (unzip) a tar.gz file, right-click on the file you want to extract and select “Extract”.

On Windows computers, you can use a software tool called 7zip to extract tar.gz files.

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:

Terminaltar -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:

Terminaltar -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:

Terminaltar -xf archive.tar.gz READMEoutputtar: 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:

Terminaltar -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:

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

If you don’t specify a decompression option, tar will indicate which option you should use:

outputtar: 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:

Terminaltar -tf archive.tar.gz

The output will look something like this:

outputfile1 file2 file3

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

Terminaltar -tvf archive.tar.gzoutput-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:

Terminaltar -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:

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

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

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

Quick Reference #

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

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

tar

Linuxize Weekly Newsletter

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

Subscribe

Unsubscribe anytime. We respect your inbox.

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

Từ khóa » Giải Nén File Tar.gz Linux