How to unpack tar gz files with examples

How to unpack tar gz files, your Ultimate Guide

Unpack tar gz files. It’s an important topic to learn because when we talk about Linux, it’s impossible not to notice the number of application packages compresses using tar gz files. This type of file is broad used in the open-source world, like the zip format.

What are tar and gz?

For generally acknowledged tar means “Tape Archive.”

GZip (Gzip is a traditional data compression application formerly written by Jean-loup Gailly for the GNU project). Every modern browser can manage a gzip encoded response.

So, the tar gz file format is a mixture of TAR packaging accompanied by a GNU zip (gzip) compression. Unix-based operation system generally uses this format. This type of file can include various files, and most frequently, they appear as package files, applications, or installers. On the MS-DOS platform, tgz is applied as short of tar.gz because MS-DOS can’t understand a file type including two dots and more than three characters following the dot. They add both tar and gz files. They need to be decompressed and finally expanded by a TAR utility. 

Also, the tar command supports a wide variety of compression applications such as gzip, xz, lzip, bzip2, lzma. Tar was initially being developed for building archives to put files on magnetic tape, which is why it has its signature, “Tape ARchive.”

Which command do we use to extract .tar.gz files?

To unpack tar gz on Linux, you can use the tar command, which also is used to pack.

Unpack tar gz files are straightforward because all Unix-based systems already have included the tar and gzip utilities by default.

Unpack tar gz

To unpack tar gz, we will use the tar utility:

For example, to unpack my application package:

tar –xvzf bitslovers-app.tar.gz

Let’s see the list of option that we used above: 

x – tells tar to unpack the files from the archive file
v – for verbose mode or to print out the files list from the unpacking process.
z – tells tar to decompress the files – without this option, you will have a folder full of compressed files.
f – means tar the filename you want it to work on.

How to specify the output directory?

Also, you can specify in advance which folder you would like to send the files to. So later, you don’t need to move the files for the desired folder manually:

tar –xvzf bitslovers-app.tar.gz –C /home/bitslovers/my-app

In addition, maybe you would like to check which files you have inside the tar gz files, so to achieve that goal, you can simply run the following command:

tar –tzf bitslovers-app.tar.gz

Until now, we have seen how to unpack tar gz files. Let’s see how easy it’s easy to create a tar gz file.

For example, let’s suppose to do the reverse order from our previous example above. In other words, we would like to compress our application for backup:

tar –cvzf bitslovers-app.tar.gz /home/bitslovers/my-app

Note that we need specify first all parameters from the tar command followed by the name of the tar file. So, the latest parameter is the files that you would like to archive. Also, if you would like to add more files at the same tar files, you just need to add more files or folder path at the end of that command, like the example below:

tar –cvzf bitslovers-app.tar.gz /home/bitslovers/my-app /etc/httpd/conf

Also, you can do using another approach to add various files to the tar file:

tar -cvf bitslovers-app.tar /etc/httpd/conf

The command above adds /etc/httpd/conf within a single file, called bitslovers-app.tar.  And, the options -cvf works as observes:

  • c – builds a new archive
  • v – for verbose mode, listing the files
  • f – defines the name of the file

Also, to unpack the files from a tar file, enter:

tar –xvf bitslovers-app.tar

The command above unpacks the tar file and shows a list of all files from our bitslovers-app.tar file, and the option responsible for executing the unpack is -x. In other words, -x is for extract, and -c is for compress (create the tar file).

Extract Specific Files from a tar.gz 

Sometimes, when we have a big tar gz file, we would like to extract only a specific file to avoid pulling all files and then getting the file that we want. Let’s see how we can do it:

tar -xf bitslovers-app.tar.gz filename1 filename2

Note: To unpack a specific file(s) from a tar gz file, add a space-separated list of file names to be unpacked after the tar gz file name. Also, don’t forget to give their exact names, including the path, as shown when you use the option –list (-t).

In addition, you also can unpack files from a tar.gz file using a wildcard pattern by applying the –wildcards option and using quotes to prevent the shell from interpreting it.

For instance, to unpack tar gz files whose names end in .conf, you can follow this example:

tar -xf bitslovers-app.tar.gz --wildcards '*.conf'

Also, we can use xargs to unpack tar gz files on Linux, which could help us perform complex scenarios where we need to create Shell Script to automate tasks.

It’s an excellent approach to compress the files on tar.gz format or zip file before copy the files or send to another person or server. This could help us to save time and bandwidth.

Conclusion

Before you go, I hope that you have learned how to unpack tar gz and, how to create it. And if you are willing to learn more, I would like to encourage you to read the man tar, to discover more options to use with the tar command.

Leave a Comment

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

Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.