Home » Linux » Applications » Tar Command Linux

How to Extract .tar.gz Files Using the tar Command in Linux

This guide explains how to use the tar command in Linux to extract files archived in the .tar.gz format.

Many open-source packages for Linux are supplied for download in the .tar.gz archive format.

.tar.gz archives behave much like .zip archives you might be more familiar with in that they contain and compress multiple files into a single archive file.

The .tar.gz archive looks a bit different from other files – it appears to have two file extensions. This is because it’s the combination of two processes.

tar (Tape ARchive) is a format for combining multiple files into a single archive (Originally intended for use with magnetic tape storage).

gz is the GNU Zip file format that compresses the contents of a file

.tar.gz – a tar archive compressed with gzip

The tar Command Syntax

The tar command is used to manipulate tar files:

tar OPTIONS FILE
  • OPTIONS are a list of options supplied from the below table
  • FILE is a path to the file or directory to be compressed

Options

Here are some commonly used options for the tar command for extracting and creating archives:

Option Long Form
-c –create create a new archive
-t –list list the contents of an archive
-x –extract, –get extract files from an archive
-j –bzip2 filter the archive through bzip2
-C –directory=DIR change to directory DIR
-f –file=ARCHIVE use archive file or device ARCHIVE
-J –xz filter the archive through xz
-p –preserve-permissions extract information about file permissions (default for superuser)
-v –verbose verbosely list files processed
-z –gzip filter the archive through gzip

Note the two other compression formats available – .bzip2 and ex – these are less frequently used, but you still may occasionally run into them.

For the full list of options you can use with the tar command, you can check the manual by running;

man tar

Extracting .tar.gz files using the tar Command

To extract a file, pass the -x option to extract the file specified using the -f option:

tar -xf my_archive.tar.gz

The files in the archive will be extracted to the current directory.

Specifying the Compression Format

tar will usually figure out which format to use to decompress your files, but you can also specify – for example specifying gzip by passing the -z option:

tar -xzf my_archive.tar.gz

Extracting Only Specified Files

To extract only certain files from a .tar.gz archive, pass the file names at the end of the command after the archive name, separated by spaces:

tar -xf my_archive.tar.gz file1.txt file2.jpg

Listing the Contents of an Archive

To extract specific files, you’ll probably need to know what’s in the archive first. Find out by running:

tar -tf my_archive.tar.gz

Extracting to a Target Directory

Pass the -C option to specify the output directory for the files being decompressed:

tar -xf my_archive.tar.gz -C /path/to/target/dir

Compressing Files using the tar Command

If you want to create a .tar.gz archive from a directory, pass the -c option (create), specify that we wish to compress the archive using gzip with the -z option. The -f option still points to the archive path, and finally, add the path to the directory you wish to compress:

tar -czf my_new_archive.tar.gz /path/to/directory

The tar program was originally written for backing up to tape – why not check out the articles in our Backup category?

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment