Untar Tar.gz – Linux Tar Command – HowTo: Extract Tar File

Most of the Linux files that can be downloaded from the Internet are compressed with a tar, tar.gz and tar.bz2 compression formats and it is important to know how to extract such files.

The following article will help you to extract (unpack) and uncompress (untar) – tar, tar.gz and tar.bz2 files from the Linux command line.

You will learn how to list the contents of a tar archive without unpacking it and how to extract only a single file or a single directory.

File extension Description
tar Simple TAR archive without compression
tar.gz TAR archive compressed with GZIP
tar.bz2 TAR archive compressed with BZIP2

Cool Tip: No more wasted time! Download from the web and untar in one step from the Linux command line! Read more →

Untar tar, tar.gz, tar.bx2 Files

Extract a tar file:

$ tar -xvf foo.tar

Extract and uncompress a tar.gz file:

$ tar -xvzf foo.tar.gz

Extract and uncompress a tar.bz2 file:

$ tar -xvjf foo.tar.bz2
Option Description
-x Extract files from an archive
-v Verbosely list files processed
-f Specify an archive or a tarball filename
-z Decompress the contents of the compressed archive created by gzip program (tar.gz)
-j Decompress the contents of the compressed archive created by bzip2 program (tar.bz2)

List the Contents of a tar, tar.gz, tar.bz2 Files

Sometimes it is needed just to check the contents of a tarball without unpacking it.

For example, it goes without saying, that it is inadvisable to untar the whole large archive if you need for example to extract only a dingle file or a directory from it.

And of course this is possible with the Linux tar command, but firstly you need to check what is there inside the tarball without unpacking it.

List the contents of a tar file:

$ tar -tvf foo.tar

List the contents of a tar.gz file:

$ tar -ztvf foo.tar.gz

List the contents of a tar.bz2 file:

$ tar -jtvf tar.bz2
Option Description
-t List the contents of an archive

Cool Tip: There is no more need to remember all these -xvf, -xvzf, -xvif keys! This awesome bash function permits to extract any archive type with the single extract command! Read more →

Extract a Single File from a Tarball

Extract a file bar.txt, from an archive:

$ tar -xvf foo.tar bar.txt
$ tar -xzvf foo.tar.gz bar.txt
$ tar -xjvf foo.tar.bz2 bar.txt

You can also specify a path to the file:

$ tar -xvf foo.tar docs/bar.txt
$ tar -xzvf foo.tar.gz docs/bar.txt
$ tar -xjvf foo.tar.bz2 docs/bar.txt

Extract a Single Directory from a Tarball

Extract a folder, called docs, from an archive:

$ tar -xvf foo.tar docs
$ tar -xzvf foo.tar.gz docs
$ tar -xjvf foo.tar.bz2 docs

You can also extract some sub-directory:

$ tar -xvf foo.tar docs/images
$ tar -xzvf foo.tar.gz docs/images
$ tar -xjvf foo.tar.bz2 docs/images