How to Extract or Unzip tar.gz Files from the Linux Command Line?

By | October 11, 2022
How to Extract or Unzip tar.gz Files from the Linux Command Line

Storage becomes a matter of concern when you have numerous files to manage. It also becomes annoying to manage multiple files and file extensions simultaneously. A tar.gz file comes into play in such a condition. A tar.gz file functions as an archive folder. To be specific, a tar.gz file can be considered as a container for other files in a system. The best thing about this archive folder is that instead of managing all the files in your system individually, you have to manage the tar.gz folder only.

A .tar.gz file is an extension of a compressed tar file. Techies often create tar files, compress them and save them with the .tar.gz extension. Technically, a .tar file is a functional portable container for transferring files smoothly and saving up storage spaces in devices. On the other hand, .gz is an extension that comes in handy for its compression utility.

Now, what’s the procedure to extract a .tar.gz file from the Linux command line? The process is not too complicated. In this article, we will discuss how a .tar.gz file can be extracted from the Linux command line.

How to Open a .tar.gz File?

Before you extract a .tar.gz file from the Linux command line, you need to know the process of opening a .tar.gz file in your system.

Mac or Linux users can open a .tar.gz file using a command terminal only. The following command will help you open a .tar.gz file in your system:

tar -xzf tarfile

Don’t have a .tar.gz file? It’s extremely easy to do so by replacing the ‘-x’ in your command with ‘-c’. So, the command will be:

tar -czf tarfile

Anyway, Mac users can open .tar.gz files directly with the help of the Archive utility. If you know how to use the Archive utility, you can opt for that too.

On the contrary, Windows users need to install external programs to open .tar.gz files. However, this can be done with WinRar too. But installing an external program is comparatively more convenient.

What is the procedure to extract or unzip .tar.gz files in Linux?

Prerequisites

  • You must have access to a dedicated command-line/ terminal window.
  • The tar utility should be included in your system by default.
  • The gzip utility needs to be included in your system by default as well.

Extracting .tar.gz Files using gzip Utility

In this tutorial, we will extract a file- ‘test.txt’ from the Linux command line with the help of gzip utility. A Linux user needs to enter the name of the file he wants to extract in the place of ‘test.txt.’

Compressing the single file that you want to extract is your first task. The command to do so is:

gzip test.txt

Once zipping is done, you can enter the ls command to confirm the compression process. The output will show the compressed file with a .gz extension.

The gunzip command turns out to be helpful in terms of decompressing a file too. In that case, you need to enter the following command:

gunzip test.txt

As the process gets completed, use the ls command again for confirmation.

It’s also possible to compress all .txt files in a specific directory at once. That lessens your task. You can do that simply by inputting this command:

gzip *.txt

This procedure is applicable for all types of files in a system directory. You only need to replace the ‘.txt’ portion of the command with the type of file you want to compress. For instance, the command for a .docx file should be:

gzip *.docx

Extracting .tar.gz Files with tar Utility

The way of extracting or unzipping a .tar.gz file with tar utility is similar to the extracting process of other files. Type in the following command to do so:

tar –xvzf documents.tar.gz

The main command that does the job is tar. The other elements of the command instruct tar for other functions. Let’s have a glance at those functions:

x- This one gives instructions to tar to extract files from a specific zip file.

v- This element prompts tar to list out extractable files that are about to get extracted.

z- This element commands tar to decompress files.

f- This one lets tar know which file you want to extract.

The following command helps you create the list of contents of the .tar file you’re about to extract:

tar –tzf documents.tar.gz

Then, use the following command to locate the unzipped and extracted files to a specific directory that you prefer:

tar –xvzf documents.tar.gz –C /home/user/destination

This command helps you create a .tar.gz file with the help of tar utility:

tar –cvzf documents.tar.gz ~/Documents

With the following command, it becomes possible to add multiple files to a tar file:

tar -cvf documents.tar ~/Documents

Let’s check out what the elements of the command instruct tar do:

c- This element instructs tar to create a new archive.

v- This one lists out the files that are being included in the archive.

f- This element specifies file names.

Use the following command to extract files from the new tar archive you just created:

tar –xvf documents.tar

Conclusion

That’s all you need to know about extracting files from a .tar.gz file. Beginner techies should avoid using this archive extension as they are not familiar with the application of the extension. But using .tar.gz files is the best option for saving storage space in your system. Now, you have complete knowledge about extracting files from a .tar.gz extension. When you’re familiar with the process, there is no point in managing files of your system separately. That becomes a time-consuming process eventually.

So, follow the guideline thoroughly to understand the process in depth. If you want to create a new tar archive in your system, follow the previous steps to create one. We have also discussed the ways to add files to a tar archive. Alongside, the processes of zipping and unzipping tar archives have been discussed in detail in this article. So, we suggest you start using .tar.gz archives to perform your tasks efficiently.

Leave a Reply

Your email address will not be published.