How to Use the Tar Command in Linux

Tar is one of the most widely used Linux commands for compression. There are great benefits in using tar, that’s why it’s loved by the pros. Here’s all you need to get you started.

Tar stands for Tape archive and is used to compress a collection of files and folders.

In most cases, once the compression is done using tar results in a .tar file. Further compression is done using gzip which would result in a .tar.gz file.

With tar, you can compress and decompress files. Tar comes with multiple options though there are few which you may need to remember.

The advantages of tar:

  • Tar, when it comes to compression has a compression ratio of 50%, which means it compresses efficiently
  • Drastically reduces the size of packaged files and folders
  • Tar does not alter the features of files and directories. The permissions and other features remain intact while compressing
  • Tar is widely available across most common Linux versions. This is available on Android firmware as well as supported older Linux flavors.
  • Compresses and Decompresses fast
  • Easy to use

While this helps us understand tar’s benefits, one question to answer is under what scenario would you choose to use it?

  • If you are working on Linux based systems and require file compression
  • To transfer a huge collection of files and folders from one server to another
  • Take a backup of your website, data or anything else
  • To reduce the usage of space on your system, since compression will occupy less space
  • To upload and download folders

How to Use Tar in Linux

Let’s learn what basic operations you can perform by using tar. Before we start, you’ll need to SSH into your VPS server. Here’s a guide to help you!

Creating a .tar Archive File in Linux

You can create .tar compressions for a file as well as directories. An example of such an archive is:

tar -cvf sampleArchive.tar /home/sampleArchive

Here /home/sampleArchive is the directory which needs to be compressed creating sampleArchive.tar.

The command uses cvf options which stand for:

  • c – This creates a new .tar file
  • v – shows a verbose description of the compression progress
  • f – file name

Creating a .tar.gz File in Linux

If you want better compression, then you can also use .tar.gz. An example of this is:

tar -cvzf sampleArchive.tar.gz /home/sampleArchive

The additional option z represents gzip compression. Alternatively, you can create a .tgz file which is similar to tar.gz. An example of this is as shown below:

tar -cvzf sampleArchive.tgz /home/sampleArchive

Creating a .tar.bz2 File in Linux

The .bz2 file provides more compression compared to gzip. However, this would take more time to compress and decompress. To create this, you need to use the -j option. An example of the operation is:

tar -cvjf sampleArchive.tar.bz2 /home/sampleArchive

This is similar to .tar.tbz or .tar.tb2. An example of this is as shown below:

tar -cvjf sampleArchive.tar.tbz /home/sampleArchive
tar -cvjf sampleArchive.tar.tb2 /home/sampleArchive

How to Unzip .tar Files in Linux

The tar command can also be used to extract a file. The below command will extract files in the current directory:

tar -xvf sampleArchive.tar

If you want to extract to a different directory then you can use the -C option. One example is as shown below:

tar -xvf sampleArchive.tar -C /home/ExtractedFiles/

A similar command can be used to uncompress .tar.gz files as shown below:

tar -xvf sampleArchive.tar.gz
tar -xvf sampleArchive.tar.gz -C /home/ExtractedFiles/

.tar.bz2 or .tar.tbz or .tar.tb2 files can be uncompressed similarly. It would require the following command in the command line:

tar -xvf sampleArchive.tar.bz2

How to List the Contents of an Archive in Linux

Once the archive is built, you can list the contents by using a command similar to the one below:

tar -tvf sampleArchive.tar

This will display the complete list of files along with timestamps and permissions. Similarly, for .tar.gz, you can use a command like:

tar -tvf sampleArchive.tar.gz

This would also work for .tar.bz2 files as shown below:

tar -tvf sampleArchive.tar.bz2

How to Unzip a Single .tar File

Once an archive is created, you can extract a single file. One such example is as shown below:

tar -xvf sampleArchive.tar example.sh

Here example.sh is a single file which will be extracted from sampleArchive.tar. Alternatively, you can also use the following command:

tar --extract --file= sampleArchive.tar example.sh

To extract a single file from .tar.gz you can use a command similar to the one shown below:

tar -zxvf sampleArchive.tar.gz example.sh

Or alternatively:

tar --extract --file= sampleArchive.tar.gz example.sh

To extract a single file from .tar.bz2 you can use a command like this:

tar -jxvf sampleArchive.tar.bz2 example.sh

Or alternatively one like this:

tar --extract --file= sampleArchive.tar.bz2 example.sh

As you can see, the tar command has a lot of flexibility in its syntax.

How to Extract Multiple Files from .tar Archives

In case you want to extract multiple files, use the below format of the command:

tar -xvf sampleArchive.tar "file1" "file2"

For .tar.gz you can use:

tar -zxvf sampleArchive.tar.gz "file1" "file2"

For .tar.bz2 you can use:

tar -jxvf sampleArchive.tar.bz2 "file1" "file2"

Extract multiple files with a pattern

If you want to extract specific patterns of files like only .jpg from the archive, use wildcards. A sample of such command is as shown below:

tar -xvf sampleArchive.tar --wildcards '*.jpg'

For .tar.gz you can use:

tar -zxvf sampleArchive.tar.gz --wildcards '*.jpg'

For .tar.bz2 you can use:

tar -jxvf sampleArchive.tar.bz2 --wildcards '*.jpg'

How to Add Files to a .tar Archive

While you can extract specific files, you can also add files into an existing archive. To do this, we would use the -r option which stands for append. Tar can add both files and directories.

Below is an example where we are adding example.jpg into the existing sampleArchive.tar.

tar -rvf sampleArchive.tar example.jpg

We can also add a directory. In the example below, the image_dir directory is added into sampleArchive.tar

tar -rvf sampleArchive.tar image_dir

You cannot add files or folder to .tar.gz or .tar.bz2 files.

How to Verify a .tar Archive in Linux

Using tar you can verify an archive. This is one of the ways you can do it:

tar -tvf sampleArchive.tar

This cannot be applied on .tar.gz or .tar.bz2 files.

How to Check Archive Size in Linux

Once you create an archive you can check the size of it. This will be displayed in KB (Kilobytes).

Below are examples of such commands with different archive files:

tar -czf - sampleArchive.tar | wc -c
tar -czf - sampleArchive.tar.gz | wc -c
tar -czf - sampleArchive.tar.bz2 | wc -c


As you can see, tar is a truly powerful tool that every Linux enthusiast should know. You can further explore the manual pages for the tar command by executing the man tar command. We hope this article helped up your Linux game! See you in the next one.

About the author

Edward S.

Edward is Hostinger's copywriter. He's an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward's goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get Exclusive Content

Join thousands of webmasters who get our free newsletter with tips on how to drive more traffic and revenue to their websites!

Please wait...

Thank you for sign up!

More in VPS
How to Change Hostname in Ubuntu 18.04