قالب وردپرس درنا توس
Home / Tips and Tricks / How to extract files from a .tar.gz or .tar.bz2 file on Linux

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



  Bash Shell on Unity Desktop Concept
Fatmawati Achmad Zaenuri / Shutterstock.com

Tar files are compressed archives. You often encounter them when you use a Linux distribution like Ubuntu or even the terminal under macOS. To extract or unpack the contents of a TAR file, also called Tarball.

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

Files that have a .tar file. gz or an extension .tar.bz2 are compressed archive files. A file with an extension of .tar is not compressed, but it will be very rare.

The file extension .tar stands for t ape ar chive, and therefore these two file types are called tar files. Tar files date back to 1

979, when the command tar was created to allow system administrators to archive files to tape. Forty years later, we still use the command tar to extract tar files onto our hard disks. Somewhere is probably still used tar with band.

The extension suffix .gz or .bz2 indicates that the archive was compressed gzip or bzip2 compression algorithm. The command tar works fine with both file types. So it does not matter which compression method was used – and it should be available wherever you have a bash shell. You only need to use the appropriate command line options tar .

Extract files from tar files

Let's say you downloaded two music files. One file is called ukulele_songs.tar.gz the other is called guitar_songs.tar.bz2 . These files are located in the Downloads directory.

 Two Tar Files in Downloads Directory

We extract the ukulele songs:

  tar -xvzf ukulele_songs.tar.gz

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

 Extracting all files from the TAR file

The command line options we used are:

  • -x : Extract the files from the tar file.
  • -v : In-depth, list the files as they are extracted.
  • -z : Gzip: Use gzip to decompress the tar file.
  • -f : File, the name of the TAR file with which tar should work. This option must be followed by the name of the TAR file.

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

 List of ukulele songs, created in the directory

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

  tar -xvjf guitar_songs.tar.bz2 

[19456536]] Extract the guitar file tar file in the Downloads folder ” width=”644″ height=”380″ src=”/pagespeed_static/1.JiBnMqyl6S.gif” onload=”pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);” onerror=”this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);”/>

Again, the files are listed in the terminal when they are unzipped. The command-line options we used with tar for the file .tar.bz2 are:

  • -x : Extract the files from the tar file.
  • -v : In-depth, list the files as they are extracted.
  • -j : Bzip2: Use bzip2 to decompress the tar file. : File, name of the tar file tar is supposed to work with.

When we list the files in the download directory, another directory called Guitar Songs is created.

 Guitar Songs directory created in the Downloads directory

Select where to extract the files

If you want to extract the files to a location other than the current directory, you can A destination directory with -C [19459013angeben] (specified directory) option.

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

If you look in our Documents / Songs directory, the guitar song is displayed s directory has been created.

 The list of guitar songs was created in the directory Documents / Songs

. Note that the target directory already exists. tar does not create it, if this is not the case. If you need to create a directory and extract tar the files in a command, you can do this as follows:

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

The option -p (parent) causes mkdir all required parent directories created target directory was created.

Search Before Extracting to Tar Files

So far, we have only made a word of trust and extracted the files invisibly. You may want to check before the jump. You can check the contents of a file tar before extracting it with the option -t (list). It is usually convenient to conduct the output less by the command .

  tar -tf ukulele_songs.tar.gz | less 

Note that we do not need to use the option -z to list the files. We only need to add the option -z if we extract files from a file .tar.gz . Likewise, we do not need the option -j to list the files in a tar.bz2 file.

 Content of tar file headed by less

If you scroll through the output, you will see that everything in the tar file is located in a directory called ukulele songs, and in that directory there are files and other directories.

 Second View of the Content of the Tar File [weniger]

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

To extract all files from a directory in 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 /" 

 Extracting a Single Folder from the Tar File

To extract a single file, enter the path and the Name of the file.

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

 Extracting a single file from tar file

You can make a selection of files using wildcards extract * any string and any single character. The use of wildcards requires the use of the option - Wildcards .

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

 Extract songs from tar with wildcards

Extract files without extracting directories

If you do not want the directory structure in the tar file to be restored to your hard drive, use the - Strip Components option. The option - Strip Components requires a numeric parameter. The number indicates how many directories are to be ignored. Files from the ignored directories are still extracted, but the directory structure is not replicated to your hard drive.

If we specify - strip-components = 1 with our sample tar file, the ukulele songs will be displayed at the top -most directory inside the tar file will not be created on disk. The files and directories that would have been unpacked into this directory are unpacked in the destination directory.

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

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

In our example TAR file there are only two levels of directory nesting. So if we use - strip-components = 2 all files in the target directory will be extracted and no other directories will be created.

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

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

If you are the Looking at Linux manpage, you'll find that tar has become a good candidate for the title of "command with the most command line options". Luckily we can extract files from the files .tar.gz and tar.bz2 To get a good degree of granular control, we only need to remember a handful of these options.




Source link