How To Extract (Unzip) Tar Gz File - Linuxize
Có thể bạn quan tâm
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.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.
Terminaltar -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:
Terminaltar -xf archive.tar.gz -C /home/linuxize/filesIf 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 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:
Terminaltar -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:
Terminaltar -xf archive.tar.gz READMEoutputtar: 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:
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 -xzIf 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 nowListing 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.gzThe output will look something like this:
outputfile1 file2 file3If 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 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:
Terminaltar -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:
Terminaltar -xf archive.tar.gz --skip-old-filesTo overwrite files only if they are older than the archive contents:
Terminaltar -xf archive.tar.gz --keep-newer-filesQuick Reference #
| 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 |
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
tarLinuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
SubscribeUnsubscribe anytime. We respect your inbox.
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 pageTừ khóa » Giải Nén File Tar.gz Linux
-
Nén Và Giải Nén Trong Linux: Zip, Tar.gz Và 2
-
Nén Và Giải Nén File Tar, Gzip Và Zip | Cloud VPS
-
Cách Giải Nén File Tar Trên Linux - Thủ Thuật
-
Hướng Dẫn Nén, Giải Nén File Trong Linux - Bizfly Cloud
-
Nén Và Giải Nén File Bằng Lệnh TAR Trên Linux (Ubuntu) - Góc Info
-
Các Câu Lệnh Nén Và Giải Nén Gzip, Tar, Zip Trong Linux
-
Hướng Dẫn Nén Và Giải Nén File Trong Linux Zip, Tar.gz Và 2
-
Cách Giải Nén Nhanh File .tar.gz Trên Windows 10
-
Nén Và Giải Nén Trong Linux: Zip, Tar.gz Và 2 - VINASTAR.NET
-
Hướng Dẫn Nén Và Giải Nén File Tar, Gzip Và Zip Trên Linux
-
How To Extract / Unzip Tar.gz Files From Linux Command Line
-
Cách Giải Nén File .TAR, .GZ, .TAR.GZ, .TGZ Nhanh Chóng Và đơn ...
-
Lệnh Tar Trong Linux (Tạo Và Giải Nén Các File Nén)
-
Cách để Giải Nén File Tar Trong Linux - WikiHow