0
0
Linux-cliHow-ToBeginner · 3 min read

How to Extract tar.gz File in Linux Quickly and Easily

To extract a tar.gz file in Linux, use the command tar -xzf filename.tar.gz. This command decompresses and extracts the archive in one step.
📐

Syntax

The basic syntax to extract a tar.gz file is:

  • tar: the command to work with tar archives
  • -x: extract files from the archive
  • -z: filter the archive through gzip to decompress
  • -f: specify the filename of the archive
  • filename.tar.gz: the archive file to extract
bash
tar -xzf filename.tar.gz
💻

Example

This example shows how to extract a file named archive.tar.gz in the current directory. It will decompress and unpack all files inside the archive.

bash
tar -xzf archive.tar.gz
Output
file1.txt folder/file2.txt folder/subfolder/file3.txt
⚠️

Common Pitfalls

Common mistakes when extracting tar.gz files include:

  • Forgetting the -z option, which causes errors because the file is compressed with gzip.
  • Using -xf without -z on a .tar.gz file, which will fail to decompress.
  • Not specifying the correct filename or path.
  • Extracting without write permissions in the target directory.
bash
tar -xf archive.tar.gz  # Wrong: misses -z option

# Correct way:
tar -xzf archive.tar.gz
📊

Quick Reference

OptionMeaning
-xExtract files from archive
-zDecompress gzip compression
-fSpecify archive filename
-vVerbose output showing files extracted (optional)
-C Extract files into specified directory

Key Takeaways

Use tar -xzf filename.tar.gz to extract tar.gz files in one step.
Always include the -z option to handle gzip compression.
Check you have correct permissions and the right filename before extracting.
Add -v for verbose output to see extracted files.
Use -C to extract files into a different directory.