If you want to compress or decompress a file, the TAR command has got you covered. One of the most extensively used commands is TAR when it comes to compressing/decompressing files in Linux. What does TAR stand for? It stands for Tape Archive. Preferred by the professionals, the tar command in Linux is basically used to compress the files and folders into archives.

Before we get into how to use the tar command, let's see what are some of its advantages.

  • TAR in Linux is very efficient as it compresses the files up to 50%.
  • Its processing speed is quite fast
  • The data after compressing remains intact as is does not alter any directories or files
  • It is easy to use

Now that we know the advantages of the TAR command, we still don’t know why we would use it? Let's change that.

Here are some of the reasons why we might have to use the TAR command.

  • Backup data or files.
  • To transfer large files.
  • Upload/download files.

TAR imbibes two versions, namely, BSD tar, and GNU tar. 

Eventually, most of the Linux systems are pre-installed with GNU. Here is a general syntax for tar:

tar [OPERATION_AND_OPTIONS] [ARCHIVE_NAME] [FILE_NAME(s)]

Operation - here is a list of most commonly used operations:

  • --create (-c) - To create a new tar file
  • -- list (-t) - To show a list of every file including the archive file
  • --extract (-x) - To extract files from the archive all the entire archive
  • --verbose (-v) - It displays the processing of files by the TAR command 
  • --file=archive=name (-f archive-name) - Depicts the name of the archive file

When it comes to different tar options, here is a complete list of all the different commands you can use with the TAR command:

-x: it is used to extract the archive file

-c: to create an archive [tar create archive]

-t: it displays the files or lists them in the archived file

-u: This helps to archive and add to an already existing archive file

-f: it helps to create an archive file with a specific filename

-v: this helps to display verbose information

-z: it stands for zipping that notifies is that our command to create our file using gzip

-W: helps in verification of an archive file

-A: This helps in the concatenation of the archive files

-j: it is used to filter several archive tar files using tbzip

-r: updates or ads file or even a directory in the existing .tar file

File_name(s)- All the file names for extraction from the archive are mentioned in the list.

Archive_name- It shows the name of the archive file

How to use the TAR command in Linux?

Let's first try to understand the meaning of an archive file! Throughout this article, we will go through the command with tar examples.

What is an archive file?

A file consisting of one or more files including the metadata is referred to as an archive file. They are primarily used to amalgamate multiple data files into a single one.  It further results in easy storage and portability. A who doesn’t want that, right?

If we want to make a tar archive file using the tar cvf option, we use this command.

tar -cvf Archive.tar /home/sample

Here c is used to create a new .tar file, v gives the description of the compression process and f represents the file name.

How to extract files using tar cvf from the archive?

tar cvf file.tar

How to extract a gzip tar archive *.tar.gz using -xvzf?

Here is a command that helps you to extract the file from the tar archive file (tar xvf):

tar xvzf file.tar.gz

How to untar (tar unzip) a single tar file or any specific directory?

Use the mentioned command in order to enter a file in any directory you want. 

tar xvfj file.tar 

or 

$ tar xvfj file.tar -C path of file in directory

How to perform gzip compression on a tar archive by using -z?

The following command helps you to create a tar file (create tar gz) known as file.tar.gz 

tar cvzf file.tar.gz *.c

How to create a compressed tar archive file using -j?

By using this command you can easily compress and create an archive file. It will be lesser than the actual size of the gzip. Although compressing usually takes more time than gzip.

tar cvfj file.tar.tbz example.cpp

Output:

$tar cvfj file.tar.tbz example.cpp

example.cpp

$tar tvf file.tar.tbz

-rwxrwxrwx root/root        94 2021-03-05 03:01 example.cpp

How to check the size of the existing tar, tar.tbz, and tar.gz file?

If you want to display the size of any archive file in the unit of KB, use the following command:

$ tar czf file1.tar.gz | wc -c

or 

$ tar czf file2.tar.tbz | wc -c

or

tar czf file.tar | wc -c

How to untar multiple files like .tar,.tar.tbz, .tar.gz?

The below-mentioned command helps you to untar multiple files [untar command in Linux]. For instance, we are here extracting file1 and file2 from archive files.

$ tar xvf file.tar "file1" "file2" 

or 

$ tar zxvf file1.tar.gz "file1" "file2"

or 

$ tar jxvf file2.tar.tbz "file1" "file2"

How do you specify a tar file and list the contents using -tf?

The below-stated command will help to deliver the list of archive file:

tar tf file.tar

Output:

example.cpp

How to update an existing tar file?

To update an existing file, follow the below-mentioned command:

tar rvf file.tar *.c

How to apply pipe to complete your search through the Grep command?

The command will only show you the stated image or text.

$ tar tvf file.tar | grep "filename.file extension"

or

$ tar tvf file.tar | grep "text to find" 

How do you view the archive option using -tvf?

The syntax goes like this:

$ tar tvf file.tar

Output:

rwxrwxrwx root/root       191 2021-03-05 01:00 os2.c

-rwxrwxrwx root/root       218 2021-03-05 01:00os3.c

-rwxrwxrwx root/root       493 2021-03-05 01:00 os4.c

 

Click here to learn more about basic Linux commands Read more

Conclusion 

As you can see tar command is beneficial in a lot of ways. You can really explore a lot. We hope that the above-mentioned commands helped you to solve your queries and added value to your knowledge base. To start using the tar command on your Linux VPS, the above information will definitely guide you through the process. 

People also read: