This article will learn how to extract/untar tar.gz files in Linux systems through the command line using the tar command.
Many of the downloadable Linux/Unix files found on the internet are compressed using a
tar.gz format. Therefore, knowing how to open or untar
tar.gz files is very useful.
The name “Tar” stands for “Tape Archiver” because it was used to place data on storage tapes when tar was invented. A
.tar.gz file is nothing but an archive. The
tar program takes one or more files and “wraps” them into a self-contained file.
tar.gz files means to extract the contents of the
tar file (also known as a tarball). Additionally, if you want to learn how to create
tar.gz files in Linux, check out our excellent guide, “How to Create tar.gz Archive Using the tar Command on Linux.”
People new to the
.tar format usually equate it to a
.zip archive, but a tar archive is not compressed. Tar collected all the files into one package, but the files can be compressed with separate utilities.
The most often used algorithm for compressing tar files is Gzip. By convention, the name of a
tar archive compressed with
To put it simply: a file that ends in
.tar.gz is just a
.tar archive compressed with
How to Extract a tar.gz File
Most Linux distributions come with the
tar command pre-installed by default.
tar.gz file, enter the following:
tar xvzf file.tar.gz
Let’s break down this syntax. Here is what each parameter in that command means:
x: This option tells tar to extract the files.
v: Verbose output shows you all the files being extracted.
z: Tells tar to decompress the archive using gzip.
f: This must be the last flag of the command. It tells tar the name and path of the compressed file.
How to Extract a tar.gz File to a Different Directory
To uncompress the
tar.gz file and put resulted files in a different directory, say
/tmp/archive, you need to add a
-C option at the end of the command:
tar xvzf file.tar.gz -C /tmp/archive
-C option is used to specify a different directory other than the current working directory.
View a List of Files within an Archive
Sometimes you need to view the content of a tar file as it collects many files and ensures if a specific file is present.
To view a detailed table of contents for archive called
data.tar.gz, use the following syntax:
tar tf data.tar.gz
t: Used to list the content of the
f: Commanding the utility to use the file mentioned in the following argument
You can also get detailed standard output by using the
v (verbose) option.
tar tvf data.tar.gz
For more about
tar command in Linux, consult its manual page.
If you find this article helpful or have additional ideas, please use the comment box below.