27.4 C
New York

A Professional Guide to Handling .tar.gz Archives: Extraction, Specific File Extraction, and More

Published:

When working with open-source software, it is common to encounter files in the .tar.gz format on a routine basis. These files serve as a means of distributing open-source packages and are accompanied by .zip formats for convenience and compatibility.

Tar archives, often colloquially known as “tarballs,” constitute collections of files consolidated into a single archive. These archives primarily serve purposes related to backup and software distribution. On Linux and Unix systems, the “tar” command is employed to create these archives, boasting compatibility with a wide array of compression programs, including gzip, bzip2, lzip, lzma, lzop, xz, and compress. The nomenclature “Tar Archive” originates from its original function of creating archives for storing files on magnetic tape.

Gzip stands out as the preeminent compression algorithm for tar files. It is customary to denote a tar archive compressed with gzip by appending either “.tar.gz” or “.tgz” to the file name.

In summary, a file bearing the “.tar.gz” extension signifies a “.tar” archive subjected to gzip compression.

Beyond the creation of new archives, the “tar” command offers multifaceted functionalities, including the extraction of tar archives, the display of file lists contained within an archive, and the addition of supplementary files to an existing archive.

This article serves as a comprehensive guide on the extraction of tar.gz and tgz archives.

Extracting a tar.gz File

The “tar” command is inherently available on most Linux distributions and macOS systems.

To extract a tar.gz file, invoke the “–extract” (“-x”) option followed by the archive file name:

tar -xf archive.tar.gz

The “tar” command will automatically discern the compression method in use and proceed with the extraction. This command can be applied universally to extract tar archives compressed with diverse algorithms, such as “.tar.bz2.”

To enhance visibility during the extraction process, the “-v” option can be employed, prompting the “tar” command to display the names of extracted files on the terminal:

tar -xvf archive.tar.gz

By default, “tar” extracts the archive contents within the current working directory. However, it is possible to designate a specific directory for unpacking using the “–directory” (“-C”) option. For instance, to extract the contents of the archive into the “/home/linuxize/files” directory, the following command may be employed:

tar -xf archive.tar.gz -C /home/linuxize/files

For desktop users who prefer a graphical interface, file managers can be employed for extraction. Simply right-click on the desired tar.gz file and select the “Extract” option.

On Windows systems, the utility of choice for extracting tar.gz files is typically 7zip.

Extracting Specific Files from a tar.gz File

To extract particular files from a tar.gz archive, list the file names separated by spaces immediately after specifying the archive name:

tar -xf archive.tar.gz file1 file2

When extracting files, it is imperative to provide their precise names and paths as they appear in the archive, as revealed by the “tar –list” (“tar -t”) command.

The process of extracting directories from an archive mirrors that of individual files:

tar -xf archive.tar.gz dir1 dir2

Attempting to extract a non-existent file will result in an error message, as illustrated below:

tar -xf archive.tar.gz README
tar: README: Not found in archive
tar: Exiting with a failure status due to previous errors

For extracting files based on a wildcard pattern, the “–wildcards” option can be employed. By enclosing the pattern in quotes, the shell’s interpretation is preemptively avoided. For example, to extract files with names ending in “.js” (JavaScript files), the following command is utilized:

tar -xf archive.tar.gz --wildcards '*.js'

Extracting a tar.gz File from stdin

When extracting a compressed tar.gz file from stdin, typically through a pipe, it is essential to specify the decompression option. The “-z” option instructs “tar” to process archives through gzip. The example below demonstrates this procedure by downloading Blender sources via the “wget” command and piping its output to the “tar” command:

wget -c https://download.blender.org/source/blender-4.0.2.tar.xz -O - | sudo tar -xz

Neglecting to specify the decompression option will prompt “tar” to recommend the correct option:

tar: Archive is compressed. Use -z option
tar: Error is not recoverable: exiting now

Listing the Contents of a tar.gz File

To retrieve a listing of the contents within a tar.gz file, execute the “tar” command alongside the “–list” (“-t”) option:

tar -tf archive.tar.gz

The output will assume a format resembling the following:

file1
file2
file3

The inclusion of the “–verbose” (“-v”) option extends the output to encompass additional details, including file permissions, ownership, size, and timestamp:

tar -tvf archive.tar.gz
-rw-r--r-- linuxize/users       0 2023-12-15 01:19 file1
-rw-r--r-- linuxize/users       0 2023-12-15 01:19 file2
-rw-r--r-- linuxize/users       0 2023-12-15 01:19 file3

Conclusion

A tar.gz file represents a tar archive compressed using the Gzip algorithm. Extraction of a tar.gz file is facilitated through the “tar -xf” command, followed by the archive’s name.

Should you have any queries or require further assistance, please do not hesitate to leave a comment below.

Related articles

Recent articles