Do you have any idea what to do with those TAR and TAR.GZ files you downloaded? Learn how to unip and extract the contents of these files.
Compression of files is a technique to free up space on a computer. If you’re familiar with Linux packages, you’ve probably come across a TAR or TAR.GZ file. While experienced users understand what a TAR file is and how to extract it, Linux beginners may struggle to figure out what to do with one.
In this tutorial, you’ll learn how to extract and compress TAR and TAR.GZ files in Linux, as well as other popular compression and archive terminology.
gzip: The file compression algorithm GNU gzip is used to compress files. Because gzip’s file extension is GZ, you may assume that any file ending in GZ has been compressed using the gzip technique.
tar.gz: A TAR.GZ file is a compressed version of a tarball using the gzip method. Tarballs use the TAR file extension, whereas gzip uses the GZ file extension. Sometimes, instead of TAR.GZ, the TGZ file extension is used.
bzip2: bzip2 is similar to gzip. When you use bzip2 to compress a TAR file, the resulting file will have one of three extensions: TAR.BZ2, TAR.BZ, or just TBZ.
How to extract tar.gz file
The tar command is pre-installed in most Linux distributions and macOS.
Use the –extract (-x) option and the archive file name after the f option to extract a tar.gz file:
tar -xf archive.tar.gz
The tar command will detect the compression type and extract the archive automatically. The same command may be used to extract tar archives that have been compressed using different methods, such as .tar.bz2.
If you’re a desktop user who prefers not to utilize the command line, you may use your file manager instead. To extract (unzip) a tar.gz file, just right-click on it and choose “Extract.” To extract tar.gz files, Windows users will need to use the 7zip program.
The -v option makes the tar command more visible on the terminal and shows the names of the files being extracted.
tar -xvf archive.tar.gz
By default, tar extracts the contents of the archive into the current directory. To extract archive files in a specified directory, use the –directory (-C) option:
For example, to extract the contents of an archive to the /home/vsudo/files directory
tar -xf archive.tar.gz -C /home/vsudo/files
Extracting Specific Files from a tar.gz File
Append a space-separated list of file names to be extracted after the archive name to extract a specific file(s) from a tar.gz file:
tar -xf archive.tar.gz file1 file2
You must give their exact names, including the file path, when extracting files, as printed by –list (-t).
It’s the same as extracting files from an archive to extract one or more directories:
tar -xf archive.tar.gz dir1 dir2
If you try to extract a file that doesn’t exist, you’ll get an error message that looks like this:
tar -xf archive.tar.gz README
tar: README: Not found in archive tar: Exiting with failure status due to previous errors
By using the –wildcards option and quoting the pattern to prevent the shell from interpreting it, you may extract files from a tar.gz file based on a wildcard pattern.
tar -xf archive.tar.gz --wildcards '*.js'
Extracting tar.gz File from stdin
You must specify the decompression option if you are extracting a compressed tar.gz file by reading it from stdin (often over a pipe). When using -z option, tar will read the archives using gzip compression.
We’ll use the wget command to get the Blender sources and feed the result to the tar command in the following example:
wget -c https://download.blender.org/source/blender-2.90.tar.gz -O - | sudo tar -xz
If you don’t provide a decompression option, tar will recommend the following:
tar: Archive is compressed. Use -z option tar: Error is not recoverable: exiting now
Listing tar.gz file
Use the –list (-t) option to list the contents of a tar.gz file:
tar -tf archive.tar.gz
You will get the output like this:
file1 file2 file3 file4
tar will output extra information if you use the –verbose (-v) option, such as the owner, file size, timestamp, and so on:
tar -tvf archive.tar.gz
-rw-r--r-- linuxize/users 0 2021-05-15 01:19 file1 -rw-r--r-- linuxize/users 0 2021-05-15 01:19 file2 -rw-r--r-- linuxize/users 0 2021-05-15 01:19 file3
A tar.gz file is a Gzip-compressed Tar archive. Use the tar -xf command followed by the archive name to extract a tar.gz file.