How to extract files from a .tar.gz or .tar.bz2 file on Linux

Tar files are compressed archives. You will encounter them a lot when using a Linux distribution like Ubuntu or even using the Terminal on macOS. Here’s how to extract – or unmask – the contents of a tar file, also known as a tarball.

What does .tar.gz and .tar.bz2 mean?

Files with a .tar.gz or a .tar.bz2 Extension are compressed archive files. A file with only a .tar Extension is uncompressed, but these will be very rare.

the .tar Part of the file extension stands for Tmonkey Withchive, which is why these two types of files are called tar files. Tar files date back to 1979 when the tar The command was created to enable system administrators to archive files to tape. Forty years later we are still using that tar Command to extract tar files to our hard drives. Somebody is probably still using it somewhere tar with tape.

the .gz or .bz2 Extension suffix indicates that the archive has been compressed, either with the gzip or bzip2 Compression algorithm. the tar The command works fine with either type of file, so it doesn’t matter which compression method was used – and it should be available anywhere you have a bash shell. You just need to use the appropriate ones tar Command line options.

Extract files from tar files

For example, suppose you downloaded two music score files. A file is called ukulele_songs.tar.gz , the other’s name is guitar_songs.tar.bz2. These files are located in the Downloads directory.

Let’s extract the ukulele songs:

tar -xvzf ukulele_songs.tar.gz


When the files are extracted, they will be listed in the terminal window.

Extraction of all files from the tar file

The command line options we used are:

  • -x: Extract, get the files from the tar file.
  • -v: Verbose, lists files as they are extracted.
  • -With: Gzip, use gzip to decompress the tar file.
  • -F: File, the name of the desired tar file tar work with. This option must be followed by the name of the tar file.

List the files in the directory with ls and you will see that a directory called Ukulele Songs has been created. The extracted files are in this directory. Where does this directory come from? It was included in the tar File and was extracted along with the files.

Directory for ukulele songs created in the Downloads directory

Now let’s extract the guitar songs. To do this, we’ll use almost exactly the same command as before, but with one important difference. the .bz2 The extension suffix tells us that it was compressed using the bzip2 command. Instead of the-z (gzip) option we use the -j (bzip2) option.

tar -xvjf guitar_songs.tar.bz2

Extract the tar file of guitar songs in the Downloads folder

The files are also listed here when they are extracted on the terminal. To be clear, the command line options we used with tar for the .tar.bz2 File goods:

  • -x: Extract, get the files from the tar file.
  • -v: Verbose, lists files as they are extracted.
  • -J: Bzip2, use bzip2 to decompress the tar file.
  • -F: File, name of the tar file that tar should work with.

If we list the files in the download directory, we can see that another directory called Guitar Songs has been created.

Guitar songs directory created in the Downloads directory

Choose where to extract files to

If we want to extract the files to a different location than the current directory, we can use the to specify a target directory -C (specified directory) option.

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


In our Documents / Songs directory we can see that the Guitar Songs directory has been created.

Guitar song directory created in the Documents / Songs directory

Note that the target directory must already exist, tar won’t create it if it doesn’t exist. When you need and have to create a directory tar Extract the files into everything in a single command, you can do it like this:

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

the -p (Parents) option causes mkdir To create all the required parent directories, make sure that the target directory is created.

Look into tar files before extracting them

So far we’ve only taken a leap of faith and extracted the files unseen. You may want to check before you jump. You can view the content of a. check tar File before extracting it with the -t (List) option. It is usually convenient to have the output through the less Command.

tar -tf ukulele_songs.tar.gz | less

Note that we don’t have to use these -z Option to list the files. We just have to add that -z Option if we want files from a. extract .tar.gz File. Likewise, we don’t need that -j Option to list the files in a tar.bz2 File.

Contents of the tar file are passed through less

If we scroll through the output we can see that everything in the tar file is in a directory called Ukulele Songs, and that’s where files and other directories are located.

Second view of the contents of the tar file passed through less


We can see that the Ukulele Songs directory contains directories called Random Songs, Ramones, and Possibles.

To extract all of the files from a directory within a tar file, use the following command. Note that the path is enclosed in quotation marks because the path contains spaces.

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

Extract a single folder from a tar file

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

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

Extract a single file from a tar file

You can extract a selection of files using wildcards, where * stands for any character string and ? stands for any single character. The use of wildcards requires the use of the --wildcards Opportunity.

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

Extract songs from tar using wildcards

Extract files without extracting directories

If you don’t want the directory structure to be rebuilt in the tar file on your hard drive, use the --strip-components Opportunity. the --strip-components Option requires a numeric parameter. The number indicates how many directory levels should be ignored. Files from the ignored directories are still extracted, but the directory structure is not replicated to your hard drive.

When we show off --strip-components=1 with our example tar file, the top directory of the ukulele songs within the tar file is not created on the hard drive. The files and directories that would have been extracted into this directory are extracted into the target directory.

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

Extract files from the tar file with --strip-components = 1


There are only two levels of directory nesting within ours example tar file. So when we use --strip-components=2, all files are extracted to the target directory and no other directories are created.

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

Extract files from the tar file with --strip-components = 2

If you look at the Linux man page you will see that tar must be a good candidate for the “command with most command-line options” title. Fortunately, so we can extract files from .tar.gz and tar.bz2 For files with a good level of granular control, we only need to remember a handful of these options.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts