“tar” is called as tape archive a command in UNIX or Linux is one of the important command which provides archiving functionality in UNIX.

The tar command used to merge a collection of files and directories into highly compressed archive file commonly called tarball or tar, gzip and bzip in Linux. Its very widely used command to create compressed archive files and that can be moved easily from one disk to anther disk or machine to machine, in other words we can call it as backup utility and its been used in most of the Unix flavors.

In this article, let us review various tar examples including how to create tar archives (tar, tar.gz and tar.bz2), extract a single file or directory, view tar archive contents, validate the integrity of tar archives, finding out the difference between tar archive and file system, estimate the size of the tar archives before creating it and many more .

You will get best insight of how to use tar in your day to day activities.

1. Creating an archive using tar command
In this example we will come to know basic tar command using option ‘cvf ‘ to create an tar archive.
Here we are creating a tar file ‘um-archive.tar’ for a directory ‘/home/unixmantra’ in current working directory.
[[email protected] /]# tar cvf um-archive.tar /home/utechdba
/home/utechdba/
/home/utechdba/hellow.sh
/home/utechdba/tools/
/home/utechdba/tools/test1
/home/utechdba/tools/perf-info.sh
/home/utechdba/testfile1
/home/utechdba/testfile2
/home/utechdba/abcCreateINdex.sql
[[email protected] /]# ls -ld um-archive.tar
-rw-r–r– 1 root root 10240 Sep 22 22:03 um-archive.tar
[[email protected] /]#
c – Creates a new .tar archive file
        v – Verbosely list files which are processed
         f – File name type of the archive file

2. Creating Zipped archive (tgz/tar.gz)
In order to make tar ball as zipped archive , we need to use the option “z” .
z – filter the archive through gzip

In below example the command will make “um-archive.tar.gz” for a directory “/home/utechdba” in current    working directory.
[[email protected] /]# tar cvzf um-archive.tar.gz /home/utechdba
/home/utechdba/
/home/utechdba/hellow.sh
/home/utechdba/tools/
/home/utechdba/tools/test1
/home/utechdba/tools/perf-info.sh
/home/utechdba/testfile1
/home/utechdba/testfile2
/home/utechdba/abcCreateINdex.sql
[[email protected] /]# ls -ld um-archive.tar.gz
-rw-r–r– 1 root root 570 Sep 22 22:05 um-archive.tar.gz
[[email protected] /]#
Note :tar.gz &tgz both are same.

3. Creating tar.bz2 Archive File
bz2 is another compression option , which we can use with Unix tar command. its exactly similar with our earlier option of compressing using gzip but instead of “z” option we need to use “j” tar option to create bzip2 file as shown in below example of tar command in Unix.

The bz2 compression takes more time to compress and decompress files as compared to gzip which takes less time. And you will get more compressed files when compared to gzip in other space used by bz2 is less than tgz file so you will achieve more compression.
j – filter the archive through bz2
[[email protected] /]# tar cvjf um-archive.tar.bz2 /home/utechdba
/home/utechdba/
/home/utechdba/hellow.sh
/home/utechdba/tools/
/home/utechdba/tools/test1
/home/utechdba/tools/perf-info.sh
/home/utechdba/testfile1
/home/utechdba/testfile2
/home/utechdba/abcCreateINdex.sql
[[email protected] /]# ls -ld um-archive.tar.bz2
-rw-r–r– 1 root root 588 Sep 22 22:06 um-archive.tar.bz2
[[email protected] /]#

4. Listing tar archive contents
View the tar archive file content without extracting using option tvf .You can view the *.tar file content before extracting as shown below.
[[email protected] /]# tar -tvf um-archive.tar
drwxr-xr-x root/root 0 2013-09-22 22:02:52 home/utechdba/
-rw-r–r– root/root 14 2013-09-22 21:51:59 home/utechdba/hellow.sh
drwxr-xr-x root/root 0 2013-09-22 22:03:03 home/utechdba/tools/
-rw-r–r– root/root 0 2013-09-22 21:50:26 home/utechdba/tools/test1
-rw-r–r– root/root 8 2013-09-22 21:50:16 home/utechdba/tools/perf-info.sh
-rw-r–r– root/root 5 2013-09-22 21:51:44 home/utechdba/testfile1
-rw-r–r– root/root 6 2013-09-22 21:51:35 home/utechdba/testfile2
-rw-r–r– root/root 656 2013-09-22 21:49:36 home/utechdba/abcCreateINdex.sql

5. Listing tar.gz archive contents
View the *.tar.gz file content without extracting using option tvzf .You can view the *.tar.gz file content before extracting as shown below.
[[email protected] /]# tar -tvzf um-archive.tar.gz
drwxr-xr-x root/root 0 2013-09-22 22:02:52 home/utechdba/
-rw-r–r– root/root 14 2013-09-22 21:51:59 home/utechdba/hellow.sh
drwxr-xr-x root/root 0 2013-09-22 22:03:03 home/utechdba/tools/
-rw-r–r– root/root 0 2013-09-22 21:50:26 home/utechdba/tools/test1
-rw-r–r– root/root 8 2013-09-22 21:50:16 home/utechdba/tools/perf-info.sh
-rw-r–r– root/root 5 2013-09-22 21:51:44 home/utechdba/testfile1
-rw-r–r– root/root 6 2013-09-22 21:51:35 home/utechdba/testfile2
-rw-r–r– root/root 656 2013-09-22 21:49:36 home/utechdba/abcCreateINdex.sql

6. Listing tar.bz2 archive contents
View the *.tar.bz2 file content without extracting using option tvjf. You can view the *.tar.bz2 file content before extracting as shown below.
[[email protected] /]# tar -tvjf um-archive.tar.bz2
drwxr-xr-x root/root 0 2013-09-22 22:02:52 home/utechdba/
-rw-r–r– root/root 14 2013-09-22 21:51:59 home/utechdba/hellow.sh
drwxr-xr-x root/root 0 2013-09-22 22:03:03 home/utechdba/tools/
-rw-r–r– root/root 0 2013-09-22 21:50:26 home/utechdba/tools/test1
-rw-r–r– root/root 8 2013-09-22 21:50:16 home/utechdba/tools/perf-info.sh
-rw-r–r– root/root 5 2013-09-22 21:51:44 home/utechdba/testfile1
-rw-r–r– root/root 6 2013-09-22 21:51:35 home/utechdba/testfile2
-rw-r–r– root/root 656 2013-09-22 21:49:36 home/utechdba/abcCreateINdex.sql

7. Extracting Tar archive contents (un-tarring)
To extract the contents of a *.tar file the option is ‘x’ .
x – extract files from archive
[[email protected] /]# tar xvf um-archive.tar
home/utechdba/
home/utechdba/hellow.sh
home/utechdba/tools/
home/utechdba/tools/test1
home/utechdba/tools/perf-info.sh
home/utechdba/testfile1
home/utechdba/testfile2
home/utechdba/abcCreateINdex.sql

8. Extracting tar.gz archive contents
To extract the contents of a *.tar.gz file the options are ‘xz’ .
[[email protected] /]# tar xvzf um-archive.tar.gz
home/utechdba/
home/utechdba/hellow.sh
home/utechdba/tools/
home/utechdba/tools/test1
home/utechdba/tools/perf-info.sh
home/utechdba/testfile1
home/utechdba/testfile2
home/utechdba/abcCreateINdex.sql

9. Extracting tar.bz2 archive contents
To extract the contents of a *.tar.bz2 file the options are ‘xj’ .
[[email protected] /]# tar xvjf um-archive.tar.bz2
home/utechdba/
home/utechdba/hellow.sh
home/utechdba/tools/
home/utechdba/tools/test1
home/utechdba/tools/perf-info.sh
home/utechdba/testfile1
home/utechdba/testfile2
home/utechdba/abcCreateINdex.sql

10. Extracting a single file from tar archive
To extract a specific file from a tar archive, specify the file name at the end of the tar xvf command as shown below. The following command extracts only a specific file from a large tar file.
$ tar xvf backup-file.tar path/to/file
$ tar xvf utechdba-backup.tar helloworld.sh
helloworld.sh

11. Extracting a single file from tar.gz archive
we need to add ‘z’ to the above extract command “xvf”
$ tar xvzf backup-file.tar /path/to/file
[[email protected] /]# tar xvf um-archive.tar home/utechdba/hellow.sh
home/utechdba/hellow.sh

12. Extracting a single file from tar.bz2 archive
we need to add ‘j’ to the above extract command “xvf”
$ tar xvjf backup-file.tar /path/to/file
$ tar xvjf um-archive.tar .gz helloworld.sh

13. Extracting a single directory from tar archive

To extract a single directory (along with it’s subdirectory and files) from a tar archive, specify the directory name at the end of the tar xvf command as shown below. The following extracts only a specific directory from a large tar file.
$ tar xvf backup-file.tar /path/to/dir
[[email protected] /]# tar xvf um-archive.tar home/utechdba/tools/
home/utechdba/tools/
home/utechdba/tools/test1
home/utechdba/tools/perf-info.sh

14. Extracting a single directory from tar.gz archive
we need to add ‘z’ to the above extract command “xvf”
$ tar xvzf backup-file.tar /path/to/dir

15. Extracting a single directory from tar.bz2 archive
we need to add ‘j’ to the above extract command “xvf”
$ tar xvjf backup-file.tar /path/to/dir

16. Untar Multiple files from tar, tar.gz and tar.bz2 File
To extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example the below command will extract “filetest 1” “file test2” & “filetest3″ from the archive files.
# tar -xvf utechdba-backup.tar “filetest1″ “filetest2″ “filetest3″
# tar -zxvf utechdba-backup.tar.gz “filetest1″ “filetest2″ “filetest3″
# tar -jxvf utechdba-backup.tar.bz2 “filetest1″ “filetest2″ “filetest3″

17 . Extract group of files from tar, tar.gz, tar.bz2 archives using regular expression

You can specify a regular expressions , to extract files matching a specified pattern. For example, following tar command extracts all the files whose file ends with .html
$ tar xvf backup-file.tar –wildcards ‘*.html’
$ tar xvzf backup-file.tar.gz –wildcards ‘*.html’
$ tar xvjf backup-file.tar.bz2 –wildcards ‘*.html’

18. Adding a file or directory to an existing archive
You can add additional files to an existing tar archive with “r” option
[[email protected] /]# tar rvf um-archive.tar testfile-new
testfile-new
[[email protected] /]# tar rvzf um-archive.tar.gz testfile-new
Aborted
[[email protected] /]# tar rvjf um-archive.tar.bz2 testfile-new
Aborted

Adding directory is also same , we need to mention directory name in place of file name.
$ tar rvf um-archive.tar new-dir/
Note: You cannot add file or directory to a compressed archives (tar.gz & tarbz2)

19. Verify integrity of tar, tar.gz & tar.bz2 File
As part of creating a tar file, you can verify the integrity of the archive file that got created using the option “W” as shown below.
[[email protected] /]# tar tvfW um-archive.tar
tar: Skipping to next header
Verify -rw-r–r– root/root 0 2013-09-22 21:50:26 home/utechdba/tools/test1
Verify -rw-r–r– root/root 8 2013-09-22 21:50:16 home/utechdba/tools/perf-info.sh
Verify -rw-r–r– root/root 5 2013-09-22 21:51:44 home/utechdba/testfile1
Verify -rw-r–r– root/root 6 2013-09-22 21:51:35 home/utechdba/testfile2
Verify -rw-r–r– root/root 656 2013-09-22 21:49:36 home/utechdba/abcCreateINdex.sql
Verify -rw-r–r– root/root 0 2013-09-22 22:15:27 testfile-new
Verify -rw-r–r– root/root 0 2013-09-22 22:17:46 testfile-new1
Note: You cannot do verification on a compressed archive (tar.gz & tarbz2)

20. Delete a file from Tar ball
You can use the following syntax to delete a file from a tar ball
[[email protected] /]#tar –delete -f example.tar filename
# tar –delete -f um-archive.tar home/utechdba/testfile1

21. Check the Size of the tar, tar.gz and tar.bz2 Archive File
To check the size of any tar, tar.gz and tar.bz2 archive file, use the following command. For example the below commands will display the size of archive file in Kilobytes (KB).
[[email protected] /]# tar -cf – um-archive.tar | wc -l
15
[[email protected] /]# tar -czf – um-archive.tar.gz | wc -l
4
[[email protected] /]# tar -cjf – um-archive.tar.bz2 | wc -l
4

Its done ,I hope this stuff helps you a lot while working with tar. If you still need much information about tar options ,just use command “man tar” .