Linux Commands

How to List and Unzip All or Partial Files From a tar.gz on Linux?

“To conserve storage space and download bandwidth, “.tar.gz” files compress multiple file types together. A tarball is another name for the.tar file, which serves as a flexible container for other data. The extension’s “.gz” portion refers to the compression tool gzip, which is frequently employed. By combining several files into an archive, the tar command can be used to build tar archives.

Two independent processes resulted in the “.tar.gz” file since it doesn’t do compression on its own. Tar essentially does nothing more than bundle several files together. Add the very efficient gzip compression to tar to make it smaller. How to extract tar.gz files from the command line is covered in this documentation. Then, type the following commands to access and extract the “.tar.gz” file. Opening a command-line terminal is the initial step in doing this. In this article, you will discover how to use Linux’s command line to extract or unzip files from “.tar.gz” files.”

Examples to List the File From tar.gz in Ubuntu 20.04

Before extracting every file from a tar or tar.gz file, you may occasionally need to list the files on the screen. Let’s look at how tar / tar.gz files (tarballs) can be displayed on Linux systems. To view a tar file’s list, the tar command is as follows.

With the tar command, we have used the ztvf flags and provided the file name “List_File.tar.gz”. For handling “.tar.gz” files, we use the “-z” switch, and for listing the contents of archive files, we use the “-t” switch. The “-v” flag lists files processed in detail, whereas the “-f” flag utilizes the filename archive. Upon execution of this command, the files are displayed which are present in “Linux_File.tar.gz”.

To view a file’s contents without actually extracting it, use the “-t” flag with the tar command. You can notice that the output and the output of the “ls –l” command are similar.

We have taken another zip file, “Info_File.tar.gz” with the -tvf flag, as we have already discussed each flag functionality above. The list of files currently presented in the “.tar.gz” file is executed when the tar command is run.

Examples to Unzip All or Partial Files From tar.gz in Ubuntu 20.04

The tar command is required to perform several different actions, including extracting tar archives, listing the contents of an archive, and adding new files to a current archive. Here, we unzip the “.tar.gz” files with the tar commands and use another command known as gzip.

By extracting a tar file using the Gzip, Gzip extracts the file to the current directory. In this instance, the file “example_file1” is located in the home directory. We use the “ls” command to display the file.

The file “example_file1” is used in the sections below. The following command should be entered in your command prompt to gzip-compress a single file “example_file1”.

We use the command “ls” to verify that the file has been compressed after zipping it. The “.gz” extension is now present, as seen by the output.

Then, the gunzip command is used to decompress a file.

We have used the gunzip command and provided the filename “example_file1” to be extracted from the “.tar.gz” file to the simple file. For a compilation of the gunzip command, we have run the “ls” command to validate the decompression of the given file.

Once more, we have verified the file extension with the “ls” command in the downloads directory. We have three pdf files located in the download directory.

We have typed the following command of gzip to compress all the “.pdf” files in a downloads directory. The “.gz” extension is now present, as seen by the output.

A wildcard character is represented by the symbol * and denotes “any number of any characters”. All the filenames ending with the .pdf extension would be compatible with this command. Other file types, such as gzip.txt, .jpg, and.doc, can also be compressed using this method. The system creates a compressed duplicate of each file when you execute gzip on a group of files simultaneously.

It is a file part of a compressed archive that includes other files. The “.tar.gz” file was initially created by running the command:

We have created the file “Linux_File.tar.gz” in the current directory by executing the following command.

We have used the “ls” command to validate that “Linux_File.tar.gz” is compressed in the current directory. You can see the compressed file highlighted in red color.

Run the command below to inspect the contents of the “.tar.gz” file.

The above command displays the file that is present in the home directory of the “Linux_File.tar.gz”.

To extract a specific file or files from a “.tar.gz” file, add the following list of file names after the archive name, separated by spaces:

To run this command, we have specified the file name “Linux_File.tar.gz” with the exact path from where we want to extract the file. We have used the —extract (-x) option and the file name after the f flag to extract a “tar.gz” file.

When only a few files need to be extracted from a tar file, you can typically do it by listing the filenames following the tar archive as follows:

The above tar command has extracted three files from the “Info_File.tar.gz” as only these files are located in the “.tar.gz” file.

Conclusion

This tutorial demonstrates how to collaborate with “.tar.gz” files using the tar and gzip tools. Now that you are prepared, you can extract or unzip any “.tar.gz” file. While backing up a Linux file system, creating tar and gz files is a crucial step. This ensures that your backup won’t be impacted if something goes wrong with your machine, and the contents won’t become corrupt. Tar archives are compressed using Gzip in tar.gz files. The tar with different flags and the archive name are the commands to extract a “.tar.gz” file.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.