How to Extract or Unzip .tar.gz Files in Linux
How to Extract or Unzip .tar.gz Files in Linux

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

Quick Answer: To extract a .tar.gz file in Linux, run tar -xzvf file.tar.gz. To extract it into a specific folder, add -C /path/to/dir. For a single .gz file (not a tarball), use gunzip file.gz instead.

Extract a .tar.gz File (the Command)

tar -xzvf file.tar.gz

That single command handles the vast majority of cases. Here’s what each flag means:

FlagMeaning
-xExtract
-zFilter through gzip (for .gz)
-vVerbose — list files as they extract
-fUse the following file name

Don’t want the file list scrolling past? Just drop the v: tar -xzf file.tar.gz.

Extract to a Specific Directory

By default, files extract into the current directory. Use -C to send them elsewhere (the target folder must already exist):

mkdir -p /opt/myapp
tar -xzvf file.tar.gz -C /opt/myapp

Extract Other Archive Types

File typeCommand
.tar.gz / .tgztar -xzvf file.tar.gz
.tar (uncompressed)tar -xvf file.tar
.tar.bz2tar -xjvf file.tar.bz2
.tar.xztar -xJvf file.tar.xz
.gz (single file)gunzip file.gz

Modern versions of tar can also auto-detect the compression, so tar -xvf file.tar.gz often works without the -z — but including it never hurts.

Extract a Single .gz File (gunzip)

A plain .gz file (like access.log.gz) is not a tarball — it’s a single compressed file. Use gunzip:

gunzip file.gz          # decompresses to "file" and removes the .gz
gzip -dk file.gz        # -d decompress, -k keep the original .gz
zcat file.gz            # print contents without decompressing to disk

List Contents Without Extracting

To see what’s inside before you extract, replace -x with -t:

tar -tzvf file.tar.gz

Extract Only Specific Files

# Extract one file or folder from the archive
tar -xzvf file.tar.gz path/inside/archive.txt

# Extract all files matching a pattern
tar -xzvf file.tar.gz --wildcards '*.conf'

Common Errors & Fixes

  • “gzip: stdin: not in gzip format” — the file isn’t actually gzipped. Try tar -xvf file.tar (no -z), or check the real type with file archive.
  • “tar: Error is not recoverable: exiting now” — usually a corrupted or partial download; re-download the file.
  • “Cannot open: Permission denied” — extract to a directory you own, or use sudo for system paths.
  • “tar: file.tar.gz: Cannot open: No such file or directory” — check the filename/path and that you’re in the right folder (ls).

Bonus: Create a .tar.gz Archive

# Compress a folder into a tar.gz
tar -czvf archive.tar.gz /path/to/folder

Just swap -x (extract) for -c (create). Need the ZIP format instead? See how to zip a file in Linux, and browse more commands in our Essential Linux Commands guide.

Frequently Asked Questions

How do I extract a tar.gz file in Linux?

Run tar -xzvf file.tar.gz in the terminal. Add -C /path to extract into a specific directory.

What is the difference between .gz and .tar.gz?

A .gz file is a single gzip-compressed file (use gunzip). A .tar.gz is a tar archive of many files that’s then gzip-compressed (use tar -xzvf).

How do I unzip a tar.gz to a specific folder?

Use the -C option: tar -xzvf file.tar.gz -C /target/folder. The folder must already exist.

How do I see what’s inside a tar.gz without extracting?

Run tar -tzvf file.tar.gz to list the contents.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *