Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Extract .tar.gz and .tar.bz2 Archives from the Linux Command Line

Unpack archives, pick a target folder, extract a single file, and also handle .gz and .bz2 files.

VPS and Cloud Servers1 min read6 screenshots

Extract a .tar.gz file

tar xvzf file.tar.gz
Extracting a tar.gz file
  • x extracts.
  • v shows the file names (verbose).
  • z filters the archive through gzip.
  • f names the archive file.

Extract to another folder

Use -C and the folder. It must exist:

tar xvzf file.tar.gz -C /path/to/somedirectory
Extracting to a chosen directory

Extract one file only

Add the name of the file as it is written in the archive:

tar -xvzf file.tar.gz test.doc
Extracting a single file

List the content first

tar -tvf file.tar.gz
Listing the content of an archive

Extract a .gz file

A plain .gz file is a single compressed file and not an archive:

gunzip file.gz
Unpacking a gz file

Extract a .tar.bz2 file

Use j in place of z:

tar xvjf file.tar.bz2
Extracting a tar.bz2 file

Extract .tar.xz files

Use J:

tar xvJf file.tar.xz

Modern tar can detect the compression by itself, so tar xvf file.tar.gz often works too.

Quick recap

  • tar xvzf for .tar.gz, tar xvjf for .tar.bz2.
  • -C folder picks the target.
  • tar -tvf lists what is inside.