tar command in Linux is stand for “tape archive”, this is free tool that is widely used in Linux to compressed files and directories and the archive file is resulted is called tar, gzip, bzip or tarball.

Through this article we will try to explain one by one tar command in linux example with various purpose that might be help you to understand how to use tar command.

1.Create tar achive.

This example tar command will create archive serverfloor-17-06-09 for directory /home/serverfloor . You can see the following command.

This is the following tar command attribute we have used :

c – create new tar archive file

v – verbosely to display the progress of creating tar archive file

f – type of filename.

2.Compress entire directory using tar command but exclude some file or sub directory

Sometime you have to compress entire directory using tar command but not all the files that are in directory,  you can use –exclude with certain directories or files that you want to exclude.

for example : let’s say you want to compress directory /home/games but don’t want to compress /home/games/assasincreed , then you can use –exclude, here to do it .

3.How to create tar.bz2 archive file.

The best of point to use tar.bz2 , it is create the best compress result file even more smaller than gzip, but tar.bz2 take a little bit more times than gzip which takes less time. You can use options -j to create highly compressed archive file.

4.Extract or untar tar archive file

Untar is the process to uncompress the tar archive file, you can use options -x to untar or uncompress the archive file. the untar destination directory is default use same directory of tar archive file, but you can change destination directory using -C option .

This following command also applies to all archive filetype such as tar.bz2, tar.gz.

5.Untar certain single file from archive file

Sometime you only need single certain file from tar.gz file and you don’t want to extract entire file in archive file you can use the following command.

In this example, you will extract file config.cfg from archive file games.tar.gz . this following command also applies to all archive filetype like tar.bz2 , gzip, tar . etc

6. Untar multiple files from archive file

You can extract multiple files from archive file, see the following command to extract two files cfg1.txt and cfg2.txt

7. Untar or uncompress group of files using wildcard.

Sometime you need to extract some of files using wildcard, for example you need to extract all file which have file extension .php . See following command

8. Add new file or directory to the existing archive file

You have one archive file, but sometime you need to add new file or directory into that existing archive file. you can use option -r ( append ) . see following command

On that following command will add mypass.txt file into archive file serverfloor.tar

9. How to verify your archive file before extract them

Before extract any archive file , sometime you need to verify it first, you can use option W ( verify ) , but you cannot verify archive file with the extension file tar.gz and tar.bz2. Please see the following command.

That’is the tar command in Linux example, hope that example are enough for your need. and if you want to read all information about tar command you can run command man tar.

Leave a Reply

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