Linux logo

What is tar command in Linux?

The Linux tar command is used to combine many files into one file also called archiving. Additionally, it is utilized to compress files to decrease the disk space necessary to store these files and also to make it simple to share several files over the net. The tar utility may also be used to decompress a compressed file to recoup the original data.

“Tar” stands for tape archive. It’s an archiving file format, that’s the most widely used tape drive backup command used by the Linux/Unix system.

It allows you to quickly get a group of files and put them into a compressed archive file commonly referred to as tarball, or tar, gzip, and bzip in Linux. The algorithm used for compressing of .tar.gz and .tar.bz2 is gzip or bzip algorithms.

On Unix-like operating systems, the tar command creates, maintains, modifies, and extracts files which are archived in the tar format.

Tar was initially developed in the early days of UNIX for the purpose of backing up files to tape-based storage devices. It was later formalized as part of the POSIX standard, and now is used to collect, distribute, and archive files, while maintaining file system features such as group and user permissions, access and modification dates, and directory structures.

Further Reading:  A little about Vim and how to search in it
Syntax of tar command

Following is the general way of using the tar command:

tar [-A –catenate -concatenate | c –create | d –diff -compare | –delete | r –append | t –list | –test-label | u –update | x –extract -get ] [options ] [pathname … ]

Now let us have a look at a few useful tar commands in Linux. This covers the above options used in the syntax part.

An example of creating a tar archive file

The below example command will create a tar archive test_ex.tar to get a directory /home/data in the current working directory. See the case command in action.

$ tar -cvf test_ex.tar /home/data/

Let us discuss each option that we’ve used in the preceding command for creating a tar archive.

  • C — Creates a new .tar
  • v – By using this option, you can see the progress of .tar file
  • f — File name type of the archive.
Create tar.gz Archive File

By default, the tar archive isn’t compressed. But if you want you can compress the contents of the archive using gzip and bzip2 algorithm.

Further Reading:  What is grep command in Linux?

To make a compressed gzip archive, we use the alternative as z. for instance the below command will make a compressed MyPictures.tar.gz file to the directory /home/MyPictures. (Note: tar.gz and tgz both are alike).

$ tar cvzf MyPictures.tar.gz /home/MyPictures
The way you may use the options

As you might notice in the option and in the above two commands, we may use the options as shown below. The four commands below produces the same result:

$ tar --create --file=test_ar.tar file1 file2 file3 file4

$ tar -c -f test_ar.tar file1 file2 file3 file4

$ tar -cf test_ar.tar file1 file2 file3 file4

$ tar cf test_ar.tar file1 file2 file3 file4

So, you may single double dash with option name or single dash with the option letter.

An example of extracting a tar.gz archive

This example shows how to extract a tar.gz file:

The next command shell will help to extract tar files out a tar.gz archive.

$ tar -xvzf test_ar.tar.gz

These options are used with a little explanation:

  • x — Extract files
  • v — Verbose, print the file names as they are extracted one by one
  • Z — The document is a “gzipped” document
  • f — Use the following tar archive for the operation
Further Reading:  How to Shutdown Linux by Using Commands
How to extract files to a specific directory or path

We can extract the files into a specified directory using the parameter”-C”.

$ tar -xvzf test_ar.tar.gz -C /test/subtest/
List Content of tar Archive File

To list the contents of the tar archive file, simply run the following command with -t option. The below command will list the content of test_ar.tar.gz file.

$ tar -tvf test_ar.tar.gz

For better screening, we can use less command or grep the pipe output for searching a document. For example,

$ tar -tvz -f test1.tar.gz | grep two.mp3

The verbose option “v” provides more information about each file.