Skip to content

How to Extract (Unzip) Tar Gz Files on Linux

If you spend a lot of time in the open-source community, you’re meant to come across.tar.gz files. Open-source packages are typically available in.tar.gz and.zip formats for download.

Tar archives are created by converting a group of files into an archive with the tar command. It supports many compression programs, including gzip, bzip2, lzip, lzma, lzop, xz, and compress. Tar was originally designed to create archives to store files on magnetic tape, which is why the name “Tape ARchive.”

Gzip is the most widely used tar file compression algorithm. The name of a tar file compressed with gzip must end with.tar.gz or.tgz by convention.

In a nutshell, a file ending in.tar.gz is a gzip-compressed.tar file.

Tar can also be used to extract tar files, view a list of files contained in the archive, add new files to an existing archive, and perform a variety of other operations.

We will show you how to extract (or unzip) the tar.gz and tgz files in this tutorial.

How to Extract a tar.gz file

The tar.gz file is a Gzip compressed tar file. To extract tar gz file, use the tar -xf followed by the file name.

Most Linux like ubuntu, arch linux, debian and macOS distributions come with the tar command pre-installed by default.

To unzip a tar.gz file, use the –extract( -x) and specify the file name after the (f) option:

tar -xf archive.tar.gz

Tar will detect the type of compression and extract the file automatically. The same command can be used to extract compressed tar files that use different algorithms, such as.tar.bz2.

If you’re a desktop user and don’t want to use the command line, you can use your File Manager. To extract (unzip) a tar.gz file, simply right-click on the file and select “Extract.” To extract the tar.gz files, Windows users will need to use the 7zip utility.

It -v makes tar more visible and prints the names of the extracted files in the terminal.

tar -xvf archive.tar.gz

The contents of the archive are extracted into the current working directory by default. To extract archives to a specific directory, use the —directory(-C) option:

For example, you may use the following command to extract the contents of a file to /home/linuxguidehq/ files:

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

Extract a specific file from a tar.gz archive

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

tar -xf archive.tar.gz file1 file2

When extracting the files, you must provide the exact names, including the path, printed by --list-t).

Extracting one or more directories from an archive is equivalent to extracting archives:

tar -xf archive.tar.gz dir1 dir2

If you try to extract a file that does not exist, an error message similar to the following will be displayed:

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

You can also extract files from a tar.gz archive based on a wildcard pattern, using the --wildcardsand quoting the pattern to prevent the shell from interpreting it.

For example, to extract files whose names end in .js(Javascript files), you would use:

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

Extract a tar.gz file from stdin

If you extract a compressed tar.gz archive by reading the archive from stdin (usually through a pipeline), you must specify the unzip option. The option that tells tar to read the files via gzip is -z.

In the example below, we are downloading the sources from Blender wget and redirecting its output to the command tar

wget -c https://example-files.online-convert.com/archive/tar.gz/example.tar.gz -O - | sudo tar -xz

If you don’t specify a decompression option, it tar will indicate which option to use:

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

List the contents of a tar.gz file

To list the contents of a tar.gz file, use the --list-t):

tar -tf archive.tar.gz

output will look like this:

file1
 file2
 file3

Adding the --verbose-v) will tarprint additional information such as owner, file size, timestamp, etc.

tar -tvf archive.tar.gz
-rw-r--r-- linuxguidehq/users 0 2020-02-15 06:34 file1

conclusion

In this article, we have learned how to unzip a tar.gz file through the tar command and its different usage options. This command is easy to use and comes pre-installed on Linux systems.

Last Updated on January 30, 2024 by admin

Share this post on social

Copyright 2022 Linuxguidehq.com. All rights reserved. No part of this website may be reproduced, distributed, or transmitted in any form or by any means, including photocopying, recording, or other electronic or mechanical methods, without the prior written permission of the copyright owner.