How to untar files in Linux/Ubuntu

In Linux, a common file format is the tarball. A tarball is a compressed folder similar to a zip file. Most Linux distributions include a graphical archive manager that allows the user to extract and manage different types of archives. While GUI based packages are great, I prefer using the terminal to manipulate files. A common task I perform is extracting files from a tarball. I always have to lookup the command needed to extract different types of tarballs. This blog post is meant to be a quick reference for myself and also to get the commands to stick in my brain.

For tar.gz(or .tgz) files

These files are compressed using gZip compressor.

$ tar zxvf file.tar.gz

The options:

z: The z option tells the tar command to uncompress the file.
x: This tells tar to extract the files.
v: v stands for “verbose”. v will list all the extracted files.
f: The f option tells tar which file you want to extract.

For tar.bz2(or tbz) files

tarbz2 files are compressed using the bZip2 compressor.

Use the following command:

$ tar jxvf file.tar.tbz

As you can see, the two commands are very similar, the only difference is that in this case, we use a ‘j’ option as opposed to using ‘z’.

j: Decompresses a bzip2 file.

A simpler way

While writing this blog post, I came across a utility called dtrx(which stands for “Do The Right Extraction”) that makes extracting archives simple, regardless of the compressor used. To extract an archive, do the following:

$ dtrx file.tar.gz
$ dtrx file.tar.bz2

To install dtrx run

$ sudo apt-get install dtrx