Everything You Need to Know About Using Tar Command in Linux

Tar command in Linux is used to rip a collection of files/folders into a single file either called a tarball, tar or gzip. We daily work with files and folders and often do various kinds of operations on those. We often do backups of these files and folders it is really hard to take the backup of each and every working folder and kept it in a separate space, the tar command in Linux is used in these cases to archive all the files/folders into a single file.

Tar command in Linux is really helpful for backups and also the transfer of files and folders with the system. There are many options in the tar command which I will explain to you in this blog which includes how to create a tar file, how to extract a tar file, how to create a gzip archive how to list all the contents of a tar file, etc.

The most common flags used in tar commands are


c= This flag is used to create a new archive file.
f= This is the file name type of the archive file.
v= This is used to print the output of the tar command verbosely.
r= Used to append or update the existing tar file.

How to create a tar file in Linux?

tar -cvf backup.tar test

In this command, I am trying to create a tar file named backup.tar for my test folder in the PWD.

How to extract the contents of the tar file?

tar -xvf backup.tar

This will extract the contents inside the contents of tar file. You can also you -C flag to extract the contents of tar file to a different directory ie
Tar -xvf backup.tar -C /home/user/Downloads.This will extract the contents to the Downloads folder.

How to create a tar.gz compression?

tar -cvzf folder.tar.gz test

The -z flag is used to create a gzip compress. This command will create a tar.gz file for the folder test we have earlier created.

The same command tar -xvf filename.tar.gz is used to extract the tar.gz file also.

How to list the contents of a tar.gz file?

tar -tvf folder.tar.gz

Here the -t flag is used to list the contents inside the tar file also you can use the same command to list the contents of tar file

Use of wildcards in the tar command.

tar -xvf backup.tar.gz –wildcards ‘*.png’

As you can see we have .png files under the folder test and we have already taken a backup of that folder name backup.tar.gz.Suppose we need to only extract the .png files we need to run the above command. The same applies to any wildcards ie you can extract only .png,.txt,.PHP files only from a tar file using the –wildcards option.

How to add a file to an existing tar file.

tar -rvf backup.tar a.txt

Suppose we need to add files to existing tar for that use the -r flag used to append or update files. This command will add a.txt to the existing tar file backup.tar.
We cannot append a file to an existing tar.gz file using the -r flag.

You can create a backup of files and folders using the tar command and use command-line tools like SCP and rsync to transfer the tar file from one server to another.

How to use the Rsync command in Linux.

How to use scp command in Linux?

Summary:

In this blog, I have discussed various use cases and options of the tar command in Linux.

25 Comments

  1. I discovered your blog website on google and verify just a few of your early posts. Continue to maintain up the very good operate. I simply additional up your RSS feed to my MSN Information Reader. Looking for forward to reading extra from you afterward!?

  2. I抦 impressed, I need to say. Really not often do I encounter a weblog that抯 both educative and entertaining, and let me let you know, you’ve hit the nail on the head. Your idea is outstanding; the difficulty is one thing that not enough people are speaking intelligently about. I am very completely happy that I stumbled throughout this in my seek for one thing referring to this.

  3. Once I initially commented I clicked the -Notify me when new comments are added- checkbox and now each time a remark is added I get 4 emails with the identical comment. Is there any manner you possibly can take away me from that service? Thanks!

  4. Wow that was unusual. I just wrote an incredibly long comment butafter I clicked submit my comment didn’t showup. Grrrr… well I’m not writing all that over again. Anyways, just wanted to say excellent blog!Here is my blog … coping with eczema

Leave a Reply

Your email address will not be published. Required fields are marked *