Linux tar.gz archive: easy compression and decompression

Linux tar.gz archive tutorial

Under Linux, it is very common to come across compressed archives in "tar.gz" format, but how do you decompress a tar.gz archive to obtain its contents? Or how do you create your own tar.gz archive to share data? This tutorial box article will answer both questions.

If you often work under LinuxYou've probably noticed that the "tar.gz" format is widely used! It's more widely used than the "ZIP" format, which is more common on Windows.

In this example, I'm using a Debian 11 machine, but you can use any other Linux distribution.

1. Compress to tar.gz under Linux

In the majority of cases, we prefer to decompress tar.gz archives downloaded from the Internet. However, it's always useful to know how to compress data in this format.

The command below creates an archive named "archive.tar.gz", adding the contents of a folder named "tutobox". You can specify the path to a folder or a file. For your information, the output file, i.e. the compressed archive, is sometimes nicknamed "tarball".

tar -czvf archive.tar.gz /var/www/tutobox

A few details about these options in our tar.gz archive to make it clearer in your mind:

  • -c : créer une archive
  • -z : compresser avec l’algorithme GZIP (le plus courant)
  • -v : afficher des informations dans la console, pour voir ce qu’il se passe
  • -f : nommer l’archive tar.gz de sortie

You can also add multiple files or folders to the same tar.gz archive. All you have to do is specify the path to the various elements to be included. In this example, we add the folder "/var/www/tutobox" and the file "/home/tutobox/config.txt" to the archive.

tar -czvf archive.tar.gz /var/www/tutobox /home/tutobox/config.txt

2. Uncompressing a tar.gz archive under Linux

For unzip a tar.gz archive under Linux and get its contents, we'll use the "tar" command with a few options. Firstly, the -x "corresponding to " -extract to extract the data, and the "-" option to extract the data.f "This is where you specify the file name.

Here's an example:

tar -xf archive.tar.gz

The contents of the archive will be extracted to the current directory, where you are currently located. To make the command a little more talkative and display the extracted file names in the console, you need to add the "-" option.v "like this:

tar -xvf archive.tar.gz

Finally, to extract data from the tar.gz archive to a specific directory rather than the current one, there's also a suitable option : the "-C" option corresponding to "-directory. In this example, the content is sent to " /home/tutobox" .

tar -xvf archive.tar.gz -C /home/tutobox

There are other options for compressing and decompressing tar.gz files under Linux, but we've just seen the main options for basic needs. To learn more about this command, please consult the command help.

tar --help

tar.gz archive Linux

3. Discovering the tar.gz format

A TAR file corresponds to an archive made up of several files and/or folders, which we compress using the "GZIP" compression algorithm, giving our compressed archive the "tar.gz" extension. In this way, a TAR file can rely on other algorithms for the "compression" part, giving rise to other extensions.

For example, you can find :

  • tar.bz2 pour une archive TAR compressé avec l’algorithme bzip2
  • tar.7z pour une archive TAR compressé avec l’algorithme 7-Zip

Still very popular today, the TAR program is not a recent solution: the initial version dates back to January 1979. Initially, the term "TAR" stood for " Tape Archiver" can be translated as follows: tape archiver.

After reading this article, you'll know how to compress and decompress data in "tar.gz" format on a Linux environment!

Resources :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *