How to Use the Linux Tar Command

How to Use the Linux Tar Command

The Linux tar command is one of the most commonly used Linux commands for compression. There are many benefits of using the tar command and are therefore highly appreciated by professionals. In this guide, we will teach you everything you need to know.

Tar stands for Tape archive and is used to compress one or more files and folders.

In most cases, a .tar file is created after compression with the tar command. Further compression is done using gzip, resulting in a .tar.gz file.

With the Linux tar command, you can compress files and undo the compression. Tar has many options, but there are a few options to remember.

Tar’s advantages:

  • Tar has a compression ratio of 50% when it comes to compression, ie it is efficiently compressed
  • Reduces the size of files and folders
  • Tar does not change the properties of files or folders. Permissions and other features remain constant when compacting
  • Tar is frequently used among most Linux versions. Tar is available in both Android software and supported legacy Linux distributions
  • Quick compression and undo compression
  • Easy to use

We saw the advantages of Tar. However, the next question to answer the question in which case to use tar:

  • If you are working on Linux-based systems and need a file compression
  • To transfer many files and folders from one server to another
  • To back up your website, data, or anything else
  • To reduce disk space usage on your system because less disk space is required as a result of compression
  • To upload and download folders

How to Use the Linux Tar Command

Next, you will learn the simple operations you can do using the tar command. You will need to log in to your VPS server using SSH before you begin.

Creating a .tar Archive File in Linux

You can compress .tar for both directories and a file. An archive example:

tar -cvf exampleArsiv.tar / home / exampleArsiv

In this example, the directory to be compressed is / home /exampleArsiv, resulting in exampleArsiv.tar.

The command uses the -cvf options:

  • c – Creates a new .tar file
  • v – shows a detailed description of the compression process
  • f – file name

Creating a .tar.gz File on Linux

You can also use .tar.gz if you want a better compression. An example of this:

tar -cvzf exampleArsivArchive.tar.gz / home / exampleArsiv

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

tar -cvzf exampleArsiv.tgz / home / exampleArsiv

Creating a .tar.bz2 File on Linux

The .bz2 file provides more compression than gzip. However, compression and undoing will take longer. You must use the -j option to create this. An example of this is:

tar -cvjf exampleArsiv.tar.bz2 / home / exampleArsiv

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

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

Opening .tar Files on Linux

The Linux tar command can also be used to extract the contents of a file. The following scripts will extract the current directory:

tar -xvf exampleArchive.tar

If you want to extract the files to a different directory, you can use the -C option. An example of this is:

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

A similar command can be used to uncompress the .tar.gz files:

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

Compression in .tar.bz2 , .tar.tbz, or .tar.tb2 files is similarly undoable . You can use the following command on the command line:

tar -xvf exampleArchive.tar.bz2

List the Contents of an Archive in Linux

After the archive is created, you can list the contents using a command similar to the following:

tar -tvf exampleArsiv.tar

This will display the list of files with time information and permissions. Similarly,  you can use a command like .tar.gz for the following:

tar -tvf exampleArsiv.tar.gz

This also works for .tar.bz2 files, as shown below :

tar -tvf exampleArsiv.tar.bz2

Opening a Single .tar File

Once the archive is created, you can extract a single file. An example of this is:

tar -xvf exampleArsiv.tar example.sh

The only file to extract from exampleArsiv.tar is example .sh. Alternatively, you can use the following command:

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

To remove a single file from .tar.gz, you can use a command like this:

tar -zxvf exampleArsiv.tar.gz example.sh

Or alternatively:

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

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

tar -jxvf exampleArchive.tar.bz2 example.sh

Or alternatively a command like this can be used:

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

As you can see, the tar command syntax has high flexibility.

Extracting Multiple Files from .tar Archives

If you want to extract multiple files, use a command in the following format:

tar -xvf exampleArsiv.tar "file1" "file2"

For .tar.gz you can use:

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

For .tar.bz2 you can use:

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

Extracting Multiple Files with a Single Pattern

Use wildcards if you only want to extract files from the archive with patterns, such as extracting .jpgfiles. An example of this command:

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

For .tar.gz you can use:

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

For .tar.bz2 you can use:

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

Adding Files to the .tar Archive

You can remove certain types of files or add files to an existing archive. To do this, you must use the -r option, which is appended for money. Tar can add both files and directories.

Here is an example where we add the example.jpg file to the existing exampleArsiv.tar :

tar -rvf exampleArchive.tar example.jpg

You can also add a directory. In the following example, the gorsel_dir directory is added to exampleArsiv.tar:

tar -rvf exampleArsiv.tar image_dir

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

Validating a .tar File on Linux

The Linux tar command can also be used to validate an archive. If this is one of the many ways you can:

tar -tvf exampleArsiv.tar

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

Controlling Archive Size in Linux

You can check the size after creating an archive. The size will be displayed in KB (Kilobytes).

Here are examples of such commands with different archive files:

tar -czf - exampleArsiv.tar | wc -c

tar -czf - exampleArsiv.tar.gz | wc -c

tar -czf - exampleArsiv.tar.bz2 | wc -c

As you can see, tar is a powerful tool that all Linux users should know.

I hope it has been a useful article.