How to use the tar command in Linux

Tar is one of the most used to compress files. It has many benefits and that is why professionals love it. Learn how to use it in this tutorial.

Tar means “Tape Archive” in English, which in Spanish would be a recording tape file, and it is used to compress a collection of files and folders.

What is the tar command used for in Linux

In most cases, once the compression is done using Tar, you will get an archive .tar. If further compression is performed using gzip, it will result in a file .tar.gz.

With the tar command, you can compress and uncompress files. This command comes with multiple options, although there are a few that you may need to remember.

The advantages of the Linux tar command:

  • Tar, when it comes to compression, has a compression ratio of 50%, which means that it compresses efficiently.
  • Drastically reduces the size of compressed files and folders.
  • Tar on Linux does not alter the characteristics of files and directories. Permissions and other details remain intact while compressing.
  • The tar command is available on most common versions of Linux. It is also available in Android firmware as well as older Linux compatible versions.
  • Quickly compress and decompress.
  • It’s easy to use.

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

  • If you are working on Linux based systems and need file compression.
  • To transfer a large number of files and folders from one server to another.
  • To make a backup of your website, data or anything else.
  • To reduce space usage on your system, as compression will take up less space
  • To upload and download folders.

How to use the tar command in Linux

Let’s see what basic operations you can perform using Tar. Before you begin, you will need to SSH into your VPS server. Here’s one to help you!

See also  How to Create a Stunning WordPress Image Gallery (In 3 Steps)

Create a .tar file on Linux

You can create .tar compressions for both an archive and directories. An example of this type of file is:

tar -cvf sampleArchive.tar /home/sampleArchive

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

This command uses the options cvf that mean:

  • c – create a new .tar file
  • v – displays a detailed description of the compression progress
  • F – file name

Create a .tar.gz file on Linux

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

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

The additional option z represents the . Alternatively, you can create a file .tgz that is similar to tar.gz. We show you an example of the latter below:

tar -cvzf sampleArchive.tgz /home/sampleArchive

Create a .tar.bz2 file on Linux

The file .bz2 provides more compression compared to gzip. However, this alternative will take more time to compress and decompress. To use it, you must use the option -j. An example of what the operation would look like is as follows:

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

This operation is similar to .tar.tbz either .tar.tb2. We show you an example below:

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

How to unzip .tar files on Linux

The Linux tar command can also be used to extract an archive. The following command will extract the files into the current directory:

tar -xvf sampleArchive.tar

If you want to extract your files to a different directory, you can use the option -C. We show you an example of this below:

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

You can use a similar command to unzip files .tar.gzas shown below:

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

The files .tar.bz2 either .tar.tbz either .tar.tb2 can be unpacked in a similar way. For this you must type the following command in the command line:

See also  How to install a theme in WordPress

tar -xvf sampleArchive.tar.bz2

How to list the contents of a file in Linux

Once you have created the file, you can list the contents using a command similar to the following:

tar -tvf sampleArchive.tar

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

tar -tvf sampleArchive.tar.gz

This would also work for files .tar.bz2 as it’s shown in the following:

tar -tvf sampleArchive.tar.bz2

How to unzip a single .tar file

Once you create a zip file, you can extract a single file from that zip. You can achieve this with the command shown below:

tar -xvf sampleArchive.tar example.sh

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

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

To extract a single file from a .tar.gz archive 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 a .tar.bz2 archive 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.

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

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”

If you want to extract from the archive specific patterns of files such as only the .jpguse the command wild cards. A sample of such a command is 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

Although you can extract specific files, you can also add new files to an existing archive. To do so, you must use the option -r what does add mean The tar command can add both files and directories.

See also  The 7 best WordPress plugins to create a wiki

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

tar -rvf sampleArchive.tar example.jpg

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

tar -rvf sampleArchive.tar image_dir

You can’t add files or folders to zips .tar.gz either .tar.bz2.

How to verify a .tar file on 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 files .tar.gz either .tar.bz2.

How to check file size in Linux

Once you create a file, you can check its size. This will be displayed in KB (Kilobytes).

Here are several examples of the command to use to check the size of different types of compressed files:

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

conclusion

As you can see, Linux Tar is a really powerful tool that every Linux enthusiast should know about. You can browse the man pages for the tar command by running the man tar command. We hope this article has helped you improve your Linux performance!

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...