Compression and Decompression of Files With Tar and Gzip 1

Compression and Decompression of Files With Tar and Gzip


The Tar and gzip are two tools used in operating systems based on Unix, like GNU/Linux, for the “packaging” and for compression of files, respectively. Although it is perfectly possible to use any of these programs individually, the the use of both at the same time it is very common and useful. So, what about learning to compress and decompress files using Tar and gzip and, consequently, to understand the working famous extension files tar.gz? This is what you you will see in this article. Break, you’ll still know how to work with the compactor bzip2.

Tar Command

Backup (backup) of the files is a necessity old. There are several ways of doing this, but in the systems operating based on Unix, one of the more traditional ways corresponds the use of the tool is Tar, which stands for Tape Archive. What the Tar does is very simple to understand: he “marshals” multiple files into one single file, this is, makes with that a single file contains several other. Thus, it is possible, for example, store in single file copies of documents contained in the folder of a user.

The resulting file of a packaging made with Tar, as is of course, extension .tar (for example, abbreviationfinder.tar), although its use is not mandatory (but it is recommended for organizational purposes). When it is necessary extract the existing content within a file .tar, of course, just turn on the program Tar. The procedures for packaging and extraction files are executed via the commands and parameters entered in terminal (shell). When a user mastered these instructions, can perform such tasks in a quick way. This is mainly due to the fact of Tar to keep the properties of the files and the directory structures the original, facilitating the location and use of each item after the extraction.

The syntax of Tar is the following:

tar [parameters] [nome_do_arquivo_tar] [arquivos_de_origem]

In the line above, tar is the command. In parameters, it is possible to use several options. Here are the main:

-c – creates a new tar file;
-t – displays the contents of a tar file;
-p – keeps the original permissions(s) of file(s);
-r – add files to a tar file;
-f – allows you to specify the tar file to be used;
-v – displays the details of the operation;
-w – prompts for confirmation before each action in the command;
-x – extracts files from a tar file;
-z – compress the tar file resulting from gzip (seen more to the front);
-C – specifies the directory of the files to be stored (note that, in this case, the letter is capitalized).

The field nome_do_arquivo_tar specifies what name the file .tar will have, and the field arquivos_de_origem sets the directory or the files which are going to become one .tar. Let’s see some examples for ease of understanding:

tar -cf legends.tar saci.txt curupira.txt

The above command creates the file legends.tar, which contains the files saci.txt and curupira.txt. Here, you should have noticed that it is possible to combine parameters. In this example, this occurred with -c and -f. In the example below, the directory hardware has all your content in the compressed in the file abbreviationfinder.tar, only the details are displayed thanks to the option -v:

tar-cvf abbreviationfinder.tar hardware

The following example lists the contents of the file abbreviationfinder.tar:

tar -tf abbreviationfinder.tar

In turn, the command below causes all files abbreviationfinder.tar to be extracted (at this point, you certainly already know the functions of the parameters x, v and f in the command):

Already in the following command, only the file saci.txt is extracted:

tar -xvf legend.tar saci.txt

An interesting thing is that, if the option -v is used twice, details such as permissions and date(s) of file(s) apareção:

tar -xvvf legends.tar saci.txt

Command gzip

The tool Tar, for you only, only serves to join multiple files into one single file. However, the program is not able to decrease the size of the resulting file, that is, to compress it. It is at this point enters the scene the gzip (GNU zip) or another archiver of your choice. If used alone, gzip makes use of the following syntax:

gzip [parameters] [filename]

Among the available parameters, we have:

-c – to extract a file to standard output;
-d – uncompresses a compressed file;the
-l – list the contents of an archive;
-v – displays details about the procedure;
-r – compact folders;
-t tests the integrity of a compressed file.

Still referring to the options of parameters, it is possible to use a numbering from 1 to 9 to indicate the level of compression. The higher the number, the greater will be the compression of the file.

Here are some examples to facilitate the understanding of the command gzip:

gzip abbreviationfinder.odt

The above command compresses the file abbreviationfinder.odt. Note that the files compressed with gzip are given the extension .gz.

gzip -d abbreviationfinder.odt.gz

The above command decompresses the file abbreviationfinder.odt.gz.

gzip -1 colorado.ods

The above procedure causes the file to colorado.ods is compressed considering the lowest level of understanding.

Using Tar and gzip

The joint use of the commands Tar and gzip is a beautiful example of consistency the phrase “the union makes the force”. Many times, it is necessary to add files and, at the same, to do with that the resulting file, in addition to contain all the other, also to be compressed. That is where that comes into play the ability of join Tar files with the compression capability of the gzip. To use both at the same time, the procedure is very simple: just apply the command tar with the parameter -z. The file resulting from this procedure will receive the extension .tar.gz.

At this point, we see a command often used in the installation programs and libraries:

tar -zxvf nome_do_arquivo.tar.gz

If you are reading this article this is the beginning, certainly you already know what the command above does, even so, let us explain to be no doubt: the letter z must be used because the file was compressed with gzip; the letter x indicates that the command should extract the file (therefore, the said statement serves to extract and decompress the file tar.gz); the letter v displays the details of the procedure; and finally, the letter f specifies which file will be used in this activity.

Note that the command is very similar to the procedure of unpacking in the previous example, with the difference that the parameter c was used in place of x, since the goal here is to create a new file, and not to make the extraction of a already existing. To extract the contents of this file, just run the command below (also displayed in the figure above):

tar -zxvf guia.tar.gz

If you want to extract only one of the files contained in the file compressed, you just have to insert it at the end of the command. For example, assume that you want to extract the file to marvin.png of guia.tar.gz. Here is what you must type:

tar -zxvf guia.tar.gz marvin.png

Using Tar and bzip2

The combination of Tar and gzip is used a lot, but not it is the only. It is also possible to use the compression algorithm bzip2, whose extension is .bz2. There are people who prefer this option for the feature of bzip2 generate smaller files than gzip, although the program to do way slower than the latter.

To use Tar with bzip2, just use the parameter -j. For example:

tar -jcvf phrases.tar.bz2 confucio.html nietzsche.html

To extract the content file, the command is:

tar -jxvf phrases.tar.bz2

If you want to use the bzip2 alone, the syntax is:

bzip2 [parameters] [filename]

The parameters are almost the same as gzip, so will not be shown here. Here is an example command:

bzip2 -d vivaldi.htm.bz2

This command decompresses the file vivaldi.htm.


Currently, it is possible to find graphical user interfaces that allow you to manipulate the options from the program Tar, gzip, bzip2, and their possible combinations simple way, just with the use of the mouse. However, to understand how to use these commands is essential for when only the linecommand is available and, of course, it is also important to so that you can understand perfectly what is that is being done. That is why this article was written.

Compression and Decompression of Files With Tar and Gzip 1