Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

Learn how to effortlessly untar targz and tarbz2 files on Linux with our comprehensive guide Gain insight into the meaning behind these file extensions, extract files efficiently, choose the desired extraction location, preview the contents before extraction, and extract files without including directories Master the art of file extraction on Linux today!

Key Takeaways

Tar files are compressed archives used in Linux distributions, like Ubuntu. They can be extracted using the tar command in the terminal.

The .tar.gz and .tar.bz2 file extensions indicate that the tar files have been compressed using the gzip or bzip2 compression algorithm.

To extract files from a tar file, use the tar command with the options -x, -v, and either -z (.gz) or -j (bz2) depending on the compression type. Use the -C option to direct the extracted files to a specific directory.

Tar files are compressed archives commonly found in Linux distributions like Ubuntu or when using the terminal on macOS. This guide will explain how to extract, or untar, the contents of a tar file, also referred to as a tarball.

What Does .tar.gz and .tar.bz2 Mean?

Compressed archive files have either a .tar.gz or .tar.bz2 extension. Uncompressed files only have a .tar extension, but these are extremely uncommon.

The .tar in the file extension represents tape archive, which is why both these file types are referred to as tar files. The tar command was introduced in 1979 to enable system administrators to store files on tape. Even after forty years, we continue to utilize the tar command to extract tar files onto our hard drives. It is likely that there are still individuals who use tar with tape.

The presence of the .gz or .bz2 extension suffix indicates that the archive has been compressed using gzip or bzip2 compression algorithms. The tar command can handle both file types, making it suitable for use in any environment with a Bash shell. Simply utilize the relevant tar command line options.

It is important to mention that the instructions provided in this article also apply to the Windows Subsystem for Linux, which enables the installation of the Bash shell in Windows 10 or Windows 11. However, there are alternative methods to open tar.gz files on Windows as well.

File Types

Extension

DAT · 7Z · BZ2 · XML · RTF · XLSX · WEBP · EPUB · MP4 · M4A · AVI · MOBI · SVG · MP3 · REG · PHP · LOG · PPTX · PDF · MPEG · WMA · M4V · AZW · LIT · TAR · TAR.GZ · RAR

Extracting Files from Tar Files

Let's assume you have downloaded two sheet music files. One file is named ukulele_songs.tar.gz, while the other is named guitar_songs.tar.bz2. Both of these files are located in the Downloads directory.

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

Let's extract (also occasionally called "untar" instead of extract) the ukulele songs:

tar -xvzf ukulele_songs.tar.gz

As the files are extracted, they are listed in the terminal window.

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

The command line options we used are:

-x: Extract, retrieve the files from the tar file.

-v: Verbose, list the files as they are being extracted.

-z: Gzip, use gzip to decompress the tar file.

Specify the tar file by using the option -f followed by the file name. After executing ls to list the directory files, you will notice the creation of a directory named "Ukulele Songs" which contains the extracted files. This directory originated from the tar file and was extracted together with the files.

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

Now, to extract the guitar songs, we will employ a similar command as before, with a crucial modification. The file we're dealing with has been compressed using the bzip2 command, indicated by the .bz2 extension suffix. Therefore, instead of utilizing the -z (gzip) option, we will employ the -j (bzip2) option to decompress the bz2 file.

tar -xvjf guitar_songs.tar.bz2

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

The extracted files are listed to the terminal once again. Just to clarify, the command line options used with tar for the .tar.bz2 file were:

-x: Extract, retrieve the files from the tar file.

-v: Verbose, list the files as they are being extracted.

-j: Bzip2, use bzip2 to decompress the tar file.

-f: File, name of the tar file we want tar to work with.

If we list the files in the Download directory we will see that another directory called Guitar Songs has been created.

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

Choosing Where to Extract the Files To

To extract the files to a different directory instead of the current one, utilize the -C option and specify the desired target directory. For example:

tar -xvjf guitar_songs.tar.gz -C ~/Documents/Songs/

Looking in our Documents/Songs directory we'll see the Guitar Songs directory has been created.

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

The target directory must already exist as tar will not create it if it is not present. If you want to create a directory and extract the files into it using one command, you can do so by executing the following:

mkdir -p ~/Documents/Songs/Downloaded && tar -xvjf guitar_songs.tar.gz -C ~/Documents/Songs/Downloaded/

The -p (parents) option causes mkdir to create any parent directories that are required, ensuring the target directory is created.

Looking Inside Tar Files Before Extracting Them

Before blindly extracting the files, it is advisable to examine them first. To review the contents of a tar file before extraction, you can use the -t (list) option. It is recommended to pipe the output through the less command for convenience.

Example:

tar -tf ukulele_songs.tar.gz | less

Notice that the -z option is not necessary for listing files. It is only required when extracting files from a .tar.gz file. Similarly, the -j option is not needed for listing files in a tar.bz2 file.

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

Scrolling through the output, it becomes apparent that all the contents of the tar file are contained within a specific directory named "Ukulele Songs." Within this directory, there exist various files and additional directories.

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

The Ukulele Songs directory includes directories named Random Songs, Ramones, and Possibles.

To extract all files from a directory within a tar file, use the command below. Please note that the path is enclosed in quotation marks as it contains spaces.

tar -xvzf ukulele_songs.tar.gz "Ukulele Songs/Ramones/"

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

To extract a single file, provide the path and the name of the file.

tar -xvzf ukulele_songs.tar.gz "Ukulele Songs/023 - My Babe.odt"

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

You can conveniently extract a range of files by leveraging wildcards. The "*" symbol represents any sequence of characters, while the "?" symbol represents any single character. The utilization of wildcards necessitates the inclusion of the --wildcards option.

tar -xvz --wildcards -f ukulele_songs.tar.gz "Ukulele Songs/Possibles/B*"

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

Extracting Files Without Extracting Directories

To prevent the recreation of the directory structure in the tar file on your hard drive, you can make use of the --strip-components option. This option requires a numerical value indicating the number of directory levels to be ignored. While the files from the ignored directories will still be extracted, the directory structure will not be replicated on your hard drive.

When we apply the --strip-components=1 option to our example tar file, the top-level directory "Ukulele Songs" will not be created on the hard drive. Instead, the files and directories that would have been extracted into that directory are directly extracted into the designated target directory.

To extract the contents, use the following command:

tar -xvzf ukulele_songs.tar.gz --strip-components=1

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

In our example tar file, there are only two levels of directory nesting. By using the --strip-components=2 option, all files will be extracted directly into the target directory, without creating any additional directories.

tar -xvzf ukulele_songs.tar.gz --strip-components=2

Mastering File Extraction on Linux: Unlock the Secrets of tarbz2 and targz Files!

If you examine the Linux man page, you will notice that tar is likely the command with the highest number of command line options. However, to efficiently extract files from .tar.gz and tar.bz2 files, we only need to remember a small set of these options.

Linux Commands

Files

tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld