
Tar files are compressed files. You will find them many times while using a Linux distribution like Ubuntu or even while using the terminal on macOS. Next, explains how to extract (or unzip) the content of a tar file, also known as tarball.
What do .tar.gz and .tar.bz2 mean?
Files that have a .tar.gz
or a .tar.bz2
extension are compressed files. A file with only one .tar
the extension is not compressed, but they will be very rare.
the .tar
part of the file extension means tmono Arkansaschive, and it is the reason why both types of files are called tar files. Tar files date back to 1979 when the tar
The command was created to allow system administrators to archive files to tape. Forty years later, we are still using the tar
command to extract tar files on our hard drives. Someone somewhere is probably still using tar
with ribbon.
the .gz
O .bz2
The extension suffix indicates that the file has been compressed, using the gzip
O bzip2
compression algorithm. the tar
The command will work perfectly with both file types, so it doesn't matter what compression method was used, and it should be available anywhere you have a bash shell. You just need to use the appropriate tar
command line options.
Extract files from Tar archives
Let's say you have downloaded two sheet music files. A file is called ukulele_songs.tar.gz
, the other is called guitar_songs.tar.bz2
. These files are located in the Downloads directory.
Let's extract the songs from the ukulele:
tar -xvzf ukulele_songs.tar.gz
As the files are extracted, are listed in the terminal window.
The command line alternatives we use are:
- -X: Extract, recover files from tar archive.
- -v: Detailed, lists files as they are extracted.
- -With: Gzip, use gzip to unzip the tar file.
- -F: File, the name of the tar file we want
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 that directory. Where did this directory come from? It was contained in the tar
file and was extracted together with the files.
Now let's extract the guitar songs. To do this, we will use almost exactly the same command as before, but with an important difference. the .bz2
The extension suffix tells us that it has been compressed using the bzip2 command. Instead of using the-z
(gzip), we will use the -j
(bzip2) option.
tar -xvjf guitar_songs.tar.bz2
One more time, files are listed in terminal as they are extracted. to be clear, the command line alternatives we use with tar
For him .tar.bz2
file were:
- -X: Extract, recover files from tar archive.
- -v: Detailed, lists files as they are extracted.
- -j: Bzip2, use bzip2 to unzip the tar file.
- -F: File, name of the tar file we want it to work with.
If we list the files in the Downloads directory we will see that another directory called Guitar Songs has been created.
Select where to extract the files
If we want to extract the files to a location other than the current directory, we can specify a destination directory using the -C
(specified directory) option.
tar -xvjf guitar_songs.tar.gz -C ~/Documents/Songs/
Looking in our Documents directory / songs, We will see that the Guitar Songs directory has been created.
Note that the destination directory must already exist, tar
will not create it if it is not present. If you need to create a directory and have tar
extract files in one command, you can do it in the following way:
mkdir -p ~/Documents/Songs/Downloaded && tar -xvjf guitar_songs.tar.gz -C ~/Documents/Songs/Downloaded/
the -p
(parents) option causes mkdir
to create the necessary home directories, making sure the destination directory is created.
Look inside tar files before extracting them
Up to now, we have made a leap of faith and have extracted the files without being seen. You may want to look before you jump. You can review the content of a tar
file before extracting it using the -t
option (ready). In general, it is convenient to channel the output through the less
command.
tar -tf ukulele_songs.tar.gz | less
Note that it is not necessary to use the -z
option to list the files. We just need to add the -z
option when we are extracting files of a .tar.gz
proceedings. In the same way, we don't need the -j
option to list the files in a tar.bz2
proceedings.
As we move through the exit, we can see that everything in the tar file is inside a directory called Ukulele Songs, and within that directory, there are files and other directories.
We can see that the Ukulele Songs directory contains directories called Random Songs, Ramones and Possibles.
To extract all files from a directory into a tar file, use the following command. Note that the path is in quotes because there are spaces in the path.
tar -xvzf ukulele_songs.tar.gz "Ukulele Songs/Ramones/"
To extract a single file, provide the path and name of the file.
tar -xvzf ukulele_songs.tar.gz "Ukulele Songs/023 - My Babe.odt"
You can extract a selection of files using wildcards, where *
represents any string of characters and ?
represents any single character. The use of wildcards requires the use of --wildcards
option.
tar -xvz --wildcards -f ukulele_songs.tar.gz "Ukulele Songs/Possibles/B*"
Extract files without extracting directories
If you don't want the directory structure in the tar file to be recreated on your hard drive, use el --strip-components
option. the --strip-components
The option needs a numeric parameter. The number represents how many levels of directories should be ignored. Files in ignored directories are still extracted, but the directory structure is not replicated on your hard drive.
If we specify --strip-components=1
with our example tar file, Ukulele songs top directory inside tar file is not created on hard drive. The files and directories that would have been extracted to that directory are extracted to the destination directory.
tar -xvzf ukulele_songs.tar.gz --strip-components = 1
There are only two levels of directory nesting within our example tar file. Then, if we use --strip-components=2
, all files are extracted to the destination directory and no other directories are created.
tar -xvzf ukulele_songs.tar.gz --strip-components = 2
If you look at Linux man page you will see that tar
has to be a good candidate for the title of “has to be a good candidate for the title of”. Fortunately, to allow us to extract files from .tar.gz
and tar.bz2
files with a good degree of granular control, we just need to remember some of these options.
setTimeout(function(){
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq = n;n.push=n;n.loaded=!0;n.version=’2.0′;
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s) } (window, document,’script’,
‘https://connect.facebook.net/en_US/fbevents.js’);
fbq(‘init’, ‘335401813750447’);
fbq(‘track’, ‘PageView’);
},3000);