How to Extract Files From a .tar.bz2 or .tar.gz File on Linux

Trending 2 months ago

Here's everything you request to cognize astir untarring a record connected Linux

The System Monitor showing nan drives connected a Linux PC. Hannah Stryker / How-To Geek

Key Takeaways

  • Tar files are compressed archives utilized successful Linux distributions, for illustration Ubuntu. They tin beryllium extracted utilizing nan tar bid successful nan terminal.
  • The .tar.gz and .tar.bz2 extensions bespeak that nan tar files are compressed utilizing nan gzip aliases bzip2 compression algorithm.
  • Extracting files from a tar record tin beryllium done utilizing nan tar bid pinch nan options -x, -v, and either -z (.gz) aliases -j (bz2) depending connected nan compression type. The extracted files tin beryllium directed to a circumstantial directory utilizing nan -C option.

Tar files are compressed archives. You'll brushwood them often while utilizing a Linux distribution for illustration Ubuntu aliases moreover while utilizing nan terminal connected macOS. Here's really to extract — aliases untar — nan contents of a tar file, besides known arsenic a tarball.

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

Files that person a .tar.gz aliases a .tar.bz2 hold are compressed archive files. A record pinch conscionable a .tar hold is uncompressed, but those will beryllium very rare.

The .tar information of nan record hold stands for tape archive, and is nan logic that some of these record types are called tar files. Tar files day each nan measurement backmost to 1979 erstwhile nan tar bid was created to let strategy administrators to archive files onto tape. Forty years later we are still utilizing nan tar bid to extract tar files connected to our difficult drives. Someone location is astir apt still utilizing tar pinch tape.

The .gz aliases .bz2 hold suffix indicates that nan archive has been compressed, utilizing either nan gzip aliases bzip2 compression algorithm. The tar bid will activity happily pinch some types of file, truthful it doesn't matter which compression method was utilized — and it should beryllium disposable everyplace you person a Bash shell. You conscionable request to usage nan due tar bid statement options.

It's worthy noting that everything successful this article besides useful connected nan Windows Subsystem for Linux, which allows you to install nan Bash ammunition wrong of Windows 10 aliases Windows 11, though location are different ways to open tar.gz files connected Windows arsenic well.

Let's opportunity you've downloaded 2 files of expanse music. One record is called ukulele_songs.tar.gz , nan different is called guitar_songs.tar.bz2 . These files are successful nan Downloads directory.

Two tar files successful nan downloads directory

Let's extract (also occasionally called "untar" alternatively of extract) nan ukulele songs:

tar -xvzf ukulele_songs.tar.gz

As nan files are extracted, they are listed successful nan terminal window.

Extraction of each files from tar file

The bid statement options we utilized are:

  • -x: Extract, retrieve nan files from nan tar file.
  • -v: Verbose, database nan files arsenic they are being extracted.
  • -z: Gzip, usage gzip to decompress nan tar file.
  • -f: File, nan sanction of nan tar record we want tar to activity with. This action must beryllium followed by nan sanction of nan tar file.

List nan files successful nan directory pinch ls and you'll spot that a directory has been created called Ukulele Songs. The extracted files are successful that directory. Where did this directory travel from? It was contained successful nan tar file, and was extracted on pinch nan files.

Ukulele Songs directory created successful Downloads directory

Now let's extract nan guitar songs. To do this we'll usage almost precisely nan aforesaid bid arsenic earlier but pinch 1 important difference. The .bz2 hold suffix tells america it has been compressed utilizing nan bzip2 command. Instead of utilizing the-z (gzip) option, we will usage nan -j (bzip2) action to unzip nan bz2 file.

tar -xvjf guitar_songs.tar.bz2
Extraction of guitar songs tar record successful Downloads folder

Once again, nan files are listed to nan terminal arsenic they are extracted. To beryllium clear, nan bid statement options we utilized pinch tar for nan .tar.bz2 record were:

  • -x: Extract, retrieve nan files from of nan tar file.
  • -v: Verbose, database nan files arsenic they are being extracted.
  • -j: Bzip2, usage bzip2 to decompress nan tar file.
  • -f: File, sanction of nan tar record we want tar to activity with.

If we database nan files successful nan Download directory we will spot that different directory called Guitar Songs has been created.

Guitar songs directory created successful Downloads directory

If we want to extract nan files to a location different than nan existent directory, we tin specify a target directory utilizing nan -C (specified directory) option.

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

Looking successful our Documents/Songs directory we'll spot nan Guitar Songs directory has been created.

Guitar songs directory created successful Documents/Songs directory

Note that nan target directory must already exist, tar will not create it if it is not present. If you request to create a directory and person tar extract nan files into it each successful 1 command, you tin do that arsenic follows:

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

The -p (parents) action causes mkdir to create immoderate genitor directories that are required, ensuring nan target directory is created.

So acold we've conscionable taken a leap of religion and extracted nan files show unseen. You mightiness for illustration to look earlier you leap. You tin reappraisal nan contents of a tar record earlier you extract it by utilizing nan -t (list) option. It is usually convenient to pipe nan output done nan little command.

tar -tf ukulele_songs.tar.gz | less

Notice that we don't request to usage nan -z action to database nan files. We only request to adhd nan -z action erstwhile we're extracting files from a .tar.gz file. Likewise, we don't request nan -j action to database nan files successful a tar.bz2 file.

Contents of tar record piped done less

Scrolling done nan output we tin spot that everything successful nan tar record is held wrong a directory called Ukulele Songs, and wrong that directory, location are files and different directories.

Second position of contents of tar record piped done less

We tin spot that nan Ukulele Songs directory contains directories called Random Songs, Ramones and Possibles.

To extract each nan files from a directory wrong a tar record usage nan pursuing command. Note that nan way is wrapped successful quotation marks because location are spaces successful nan path.

tar -xvzf ukulele_songs.tar.gz "Ukulele Songs/Ramones/"
Extracting azygous files from tar file

To extract a azygous file, supply nan way and nan sanction of nan file.

tar -xvzf ukulele_songs.tar.gz "Ukulele Songs/023 - My Babe.odt"
Extracting azygous record from tar file

You tin extract a action of files by utilizing wildcards, wherever * represents immoderate drawstring of characters and ? represents immoderate azygous character. Using wildcards requires nan usage of nan --wildcards option.

tar -xvz --wildcards -f ukulele_songs.tar.gz "Ukulele Songs/Possibles/B*"
Extracting songs from tar pinch wildcards

If you don't want nan directory building successful nan tar record to beryllium recreated connected your difficult drive, usage nan --strip-components option. The --strip-components action requires a numerical parameter. The number represents really galore levels of directories to ignore. Files from nan ignored directories are still extracted, but nan directory building is not replicated connected your difficult drive.

If we specify --strip-components=1 pinch our illustration tar file, nan Ukulele Songs top-most directory wrong nan tar record is not created connected nan difficult drive. The files and directories that would person been extracted to that directory are extracted successful nan target directory.

tar -xvzf ukulele_songs.tar.gz --strip-components=1
Extracting files from tar record pinch --strip-components=1

There are only 2 levels of directory nesting wrong our illustration tar file. So if we usage --strip-components=2, each nan files are extracted successful nan target directory, and nary different directories are created.

tar -xvzf ukulele_songs.tar.gz --strip-components=2
Extracting files from tar record pinch --strip-components=2

If you look astatine nan Linux man page you'll spot that tar has sewage to beryllium a bully campaigner for nan title of "command having nan astir bid statement options." Thankfully, to let america to extract files from .tar.gz and tar.bz2 files pinch a bully grade of granular control, we only request to retrieve a fistful of these options.

Source Tutorials
Tutorials