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:

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:

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:

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:

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

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:

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

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

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

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:

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

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

Opening a Single .tar File

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

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

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

Or alternatively:

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

Or alternatively a command like this can be used:

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:

For .tar.gz you can use:

For .tar.bz2 you can use:

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:

For .tar.gz you can use:

For .tar.bz2 you can use:

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 :

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

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:

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:

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

I hope it has been a useful article.