Skip navigation.

Using TAR Command

So, you've got a tarball and want to know how to extract it, huh?

tar archiveSome background info about tar: In the olden days, people primarily used tape devices to store and retrieve data. Tape drives still exist and folks use them to archive data. The name tar is derived from from tape archive. The name of the program that creates and extracts tar files also has the same name - tar. Tarball refers to the archive file. They usually have the extension filename.tar.gz.

Extracting tar files with extension .tar.gz: To extract a tar.gz file use the below command. Assuming you have a tarball by name archived-file.tar.gz:

tar -zxvf archived-file.tar.gz

In the above command the options have the following meaning.

  • z - filter the archive through gzip. gzip aka GNU Zip is a program used to compress files. When you use -z option, gzip will be used in tandem with tar.
  • x - extract files from an archive. Pretty self explanatory, huh?
  • v - verbosely list files processed. The -v option prints information on the screen when the files are being extracted from the archive. If you do not use this option, tar still extracts files silently.
  • f - use archive file. This option tells tar to extract from a file. The -f switch is required.

Extracting tar files with extension .tar.bz2: some tarballs are archived using the bz2 compression utility. Assuming you have a tarball by name
archived-file.tar.bz2:

tar -jxvf archived-file.tar.bz2

The only difference between the above two commands is the compression program switch. In the above example, we have used the -j option. The -j option instructs tar to use bz2 compression program to extract the files. In our previous example, we used the -z option to instruct tar to use gunzip to decompress the archive.

Extracting tar files with extension .tar: some tarballs may not have any extensions. These type of archives are not compressed at all. It is convenient to transfer a single archived file to a different computer than a bunch of files. You will feel this convenience if you use a FTP client to transfer a set of files to an FTP server. This holds good for downloading too. That is why we have tarballs. Assuming you have a tarball by name
archived-file.tar:

tar -xvf archived-file.tar

The file extensions are just indicators of the file type. It is not mandatory to use .tar, .tar.gz or .tar.bz2 file extensions to represent respective archive types. If you follow this file naming convention, it will be easier for others to determine the file type just by looking at the name. For argument's sake, let us assume that somebody sent you an archive with the file name archive without any extensions to it. How do you know what command to use to extract the archive? Use the file command to determine the correct file type.

file archive

The system reads the file and tells us what type of file it is.
archive: bzip2 compressed data, block size = 900k

The above command prints the information about the file type. Now we know that the file is bzip2 compressed archive. Do you know what command to use to extract this type of tarball?

You can also view the files in the archive without extracting it.

tar -tf archive-file.tar.bz2

The above command prints the files contained in the archive file -archive-file.tar.bz2. The -t option instructs tar to list the contents of the archive.

[sudheer@localhost arc]$ tar -tf archive-file.tar.bz2
abc.txt
xyz.txt

You can also extract selected files from an archive. In the above example, the archive-file.tar.bz2 contains two text files. Let us extract only abc.txt from the archive.

tar -jxvf archive-file.tar.bz2 abc.txt

Although the archive contains two files, the above command extracts only abc.txt from archive-file.tar.bz2.

So, you want to know how to create tarballs, huh?

Let us now discuss how you can create tar archives. And compress them too. To create a tarball we will just replace the -x with -c. We know that, -x stands for extract. Similarly, -c stands for create.

For the sake of this example I am assuming that you have two files named abc.txt and xyz.txt. We will use these file names throughout this post. To create a tarball from these two files use the below command:

tar -cvf archive-file.tar abc.txt xyz.txt

Alternatively you can use wild cards to achieve the same result instead of typing every file name from which the tarball has to be created.
tar -cvf archive-file.tar *txt

To use bzip2 compression while creating the archive use the -j option.

tar -cjvf archive-file.tar.bz2 *txt

To use gzip compression while creating the archive use the -z option.

tar -czvf archive-file.tar.gz *txt

I use tar to backup my home directory frequently. The path to my home directory is
/home/sudheer. The below tar command creates an archive of all the directories and sub directories under /home/sudheer. To do this you have to change directory to at least one hierarchical level up.

tar -jxvf my-home-directory.tar.bz2 /home/sudheer

I hope this post is helpful to those of us who are learning compressing and archiving on GNU/Linux. I will look forward for your comments. You can also use the forum to discuss this topic.



Nice write-up!

Clear, concise and explanation of the options is appreciated. Probably all the 'tar' anyone needs to know 95% of the time. Should I ever write another install tute for something, I'll openly reference this article for the tar stuff. Good job.

Thanks Destry

Thanks Destry.

With warm regards,
Sudheer

I know how to search a tar

Thank you, Now I know how to search inside a tar file.

I am so glad the post helped

I am so glad the post helped you.

With warm regards,
Sudheer

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Use the special tag [adsense:format:group:channel] or [adsense:flexiblock:location] to display Google AdSense ads.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.