How to Extract tar.gz in Linux: All Formats Covered

Extract tar.gz files in Linux — handle .tar.gz, .tar.bz2, .tar.xz, and .zip formats, extract to specific directories, and handle common extraction errors.

April 22, 2026·4 min read·Damon

One command. Multiple archive formats. Here's the complete reference.


TL;DR

tar -xzf archive.tar.gz           # extract .tar.gz here
tar -xzf archive.tar.gz -C /dest  # extract to specific directory
tar -xjf archive.tar.bz2          # extract .tar.bz2
tar -xJf archive.tar.xz           # extract .tar.xz
unzip archive.zip                  # extract .zip

tar Format Flags

Extension Extract flag Example
.tar.gz or .tgz -xzf tar -xzf file.tar.gz
.tar.bz2 or .tbz2 -xjf tar -xjf file.tar.bz2
.tar.xz or .txz -xJf tar -xJf file.tar.xz
.tar (no compression) -xf tar -xf file.tar

Modern GNU tar also auto-detects the compression:

tar -xf archive.tar.gz   # no compression flag needed — auto-detected

Extract to a Specific Directory

# Extract to /opt/app/ (directory must exist)
tar -xzf archive.tar.gz -C /opt/app/

# Create directory if it doesn't exist, then extract
mkdir -p /opt/app && tar -xzf archive.tar.gz -C /opt/app/

Extract a Single File From an Archive

# First, find the file path inside the archive
tar -tzf archive.tar.gz | grep config

# Then extract just that file
tar -xzf archive.tar.gz var/app/config.yml
# Extracts maintaining the path structure

Preview Before Extracting

# List contents
tar -tzf archive.tar.gz

# With details (permissions, size, date)
tar -tvzf archive.tar.gz

# Check what directory it will create
tar -tzf archive.tar.gz | head -5

Always check before extracting to avoid an archive that dumps hundreds of files into your current directory.


Handle Archives That Extract to Root

# Archive created from /
# Extracting blindly would overwrite system files!

# Check the paths
tar -tzf system-backup.tar.gz | head
# ./etc/nginx/nginx.conf
# ./var/log/app/app.log

# Extract safely with --strip-components to remove leading path
tar -xzf backup.tar.gz --strip-components=1 -C /restore/

zip and Other Formats

# .zip
unzip archive.zip
unzip archive.zip -d /destination/
unzip -l archive.zip    # list contents

# .gz (single file, not archive)
gunzip file.gz          # decompress in place (removes .gz)
gzip -d file.gz         # same
gzip -dk file.gz        # keep original .gz file

# .bz2 (single file)
bunzip2 file.bz2
bzip2 -d file.bz2

# .xz (single file)
unxz file.xz
xz -d file.xz

Real Examples

Deploy an application from a tarball

# Check what's inside first
tar -tzf myapp-v2.1.0.tar.gz | head

# Extract to /opt
mkdir -p /opt/myapp
tar -xzf myapp-v2.1.0.tar.gz -C /opt/myapp --strip-components=1

# Verify
ls /opt/myapp/

Restore from backup

# Stop the service first
systemctl stop myapp

# Restore config from backup
tar -xzf nginx-config-backup-20260422.tar.gz -C /

# Verify
ls /etc/nginx/

# Restart
systemctl start myapp

Extract and pipe (no temp file)

# Download and extract in one line
curl -L https://example.com/release.tar.gz | tar -xzf - -C /opt/

# Same with wget
wget -qO- https://example.com/release.tar.gz | tar -xzf - -C /opt/

Common Mistakes

Mistake 1: Extracting into wrong directory

# Extracts into current directory — could mix with existing files
tar -xzf archive.tar.gz

# Safe: always specify destination
mkdir -p /tmp/extract && tar -xzf archive.tar.gz -C /tmp/extract/

Mistake 2: Not checking paths before extracting An archive might contain absolute paths like /etc/passwd. Always tar -tzf first.

Mistake 3: Wrong compression flag .tar.bz2 with -xzf (gzip flag) fails. If unsure:

tar -xf archive.tar.gz   # let tar auto-detect
file archive.tar.gz       # or check the type first

Mistake 4: Forgetting -C directory must exist -C /path fails if the path doesn't exist. Create it first with mkdir -p.


Quick Reference

# Extract to current directory
tar -xzf file.tar.gz       # .tar.gz
tar -xjf file.tar.bz2      # .tar.bz2
tar -xJf file.tar.xz       # .tar.xz
tar -xf  file.tar          # .tar

# Extract to specific directory
tar -xzf file.tar.gz -C /destination/

# List contents
tar -tzf file.tar.gz

# Extract single file
tar -xzf file.tar.gz path/to/specific/file

# zip
unzip file.zip
unzip file.zip -d /destination/

Conclusion

tar -xzf archive.tar.gz for .tar.gz. Add -C /path/ to specify destination. Always tar -tzf before extracting to see what you're getting. For auto-detection of compression type, just tar -xf without a compression flag works on modern GNU tar.


Related: How to Compress Files Linux: tar Examples — creating archives. How to Copy Files Recursively Linux — moving files after extraction.