Sunday 22 September 2013

21 Ultimate Tar command examples - Unix

"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.
[root@unixmantra /]# tar cvf um-archive.tar /home/unixmantra
/home/unixmantra/
/home/unixmantra/hellow.sh
/home/unixmantra/tools/
/home/unixmantra/tools/test1
/home/unixmantra/tools/perf-info.sh
/home/unixmantra/testfile1
/home/unixmantra/testfile2
/home/unixmantra/abcCreateINdex.sql
[root@unixmantra /]# ls -ld um-archive.tar
-rw-r--r-- 1 root root 10240 Sep 22 22:03 um-archive.tar
[root@unixmantra /]#
  • 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/unixmantra" in current working directory.
[root@unixmantra /]# tar cvzf um-archive.tar.gz /home/unixmantra
/home/unixmantra/
/home/unixmantra/hellow.sh
/home/unixmantra/tools/
/home/unixmantra/tools/test1
/home/unixmantra/tools/perf-info.sh
/home/unixmantra/testfile1
/home/unixmantra/testfile2
/home/unixmantra/abcCreateINdex.sql
[root@unixmantra /]# ls -ld um-archive.tar.gz
-rw-r--r-- 1 root root 570 Sep 22 22:05 um-archive.tar.gz
[root@unixmantra /]#

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
[root@unixmantra /]# tar cvjf um-archive.tar.bz2 /home/unixmantra
/home/unixmantra/
/home/unixmantra/hellow.sh
/home/unixmantra/tools/
/home/unixmantra/tools/test1
/home/unixmantra/tools/perf-info.sh
/home/unixmantra/testfile1
/home/unixmantra/testfile2
/home/unixmantra/abcCreateINdex.sql
[root@unixmantra /]# ls -ld um-archive.tar.bz2
-rw-r--r-- 1 root root 588 Sep 22 22:06 um-archive.tar.bz2
[root@unixmantra /]#

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.
[root@unixmantra /]# tar -tvf um-archive.tar
drwxr-xr-x root/root         0 2013-09-22 22:02:52 home/unixmantra/
-rw-r--r-- root/root        14 2013-09-22 21:51:59 home/unixmantra/hellow.sh
drwxr-xr-x root/root         0 2013-09-22 22:03:03 home/unixmantra/tools/
-rw-r--r-- root/root         0 2013-09-22 21:50:26 home/unixmantra/tools/test1
-rw-r--r-- root/root         8 2013-09-22 21:50:16 home/unixmantra/tools/perf-info.sh
-rw-r--r-- root/root         5 2013-09-22 21:51:44 home/unixmantra/testfile1
-rw-r--r-- root/root         6 2013-09-22 21:51:35 home/unixmantra/testfile2
-rw-r--r-- root/root       656 2013-09-22 21:49:36 home/unixmantra/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.
[root@unixmantra /]# tar -tvzf um-archive.tar.gz
drwxr-xr-x root/root         0 2013-09-22 22:02:52 home/unixmantra/
-rw-r--r-- root/root        14 2013-09-22 21:51:59 home/unixmantra/hellow.sh
drwxr-xr-x root/root         0 2013-09-22 22:03:03 home/unixmantra/tools/
-rw-r--r-- root/root         0 2013-09-22 21:50:26 home/unixmantra/tools/test1
-rw-r--r-- root/root         8 2013-09-22 21:50:16 home/unixmantra/tools/perf-info.sh
-rw-r--r-- root/root         5 2013-09-22 21:51:44 home/unixmantra/testfile1
-rw-r--r-- root/root         6 2013-09-22 21:51:35 home/unixmantra/testfile2
-rw-r--r-- root/root       656 2013-09-22 21:49:36 home/unixmantra/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.
[root@unixmantra /]# tar -tvjf um-archive.tar.bz2
drwxr-xr-x root/root         0 2013-09-22 22:02:52 home/unixmantra/
-rw-r--r-- root/root        14 2013-09-22 21:51:59 home/unixmantra/hellow.sh
drwxr-xr-x root/root         0 2013-09-22 22:03:03 home/unixmantra/tools/
-rw-r--r-- root/root         0 2013-09-22 21:50:26 home/unixmantra/tools/test1
-rw-r--r-- root/root         8 2013-09-22 21:50:16 home/unixmantra/tools/perf-info.sh
-rw-r--r-- root/root         5 2013-09-22 21:51:44 home/unixmantra/testfile1
-rw-r--r-- root/root         6 2013-09-22 21:51:35 home/unixmantra/testfile2
-rw-r--r-- root/root       656 2013-09-22 21:49:36 home/unixmantra/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
[root@unixmantra /]# tar xvf um-archive.tar
home/unixmantra/
home/unixmantra/hellow.sh
home/unixmantra/tools/
home/unixmantra/tools/test1
home/unixmantra/tools/perf-info.sh
home/unixmantra/testfile1
home/unixmantra/testfile2
home/unixmantra/abcCreateINdex.sql

8. Extracting  tar.gz archive contents

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

9. Extracting tar.bz2 archive contents

To extract the contents of a *.tar.bz2 file the options are ‘xj’ .
[root@unixmantra /]# tar xvjf um-archive.tar.bz2
home/unixmantra/
home/unixmantra/hellow.sh
home/unixmantra/tools/
home/unixmantra/tools/test1
home/unixmantra/tools/perf-info.sh
home/unixmantra/testfile1
home/unixmantra/testfile2
home/unixmantra/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 unixmantra-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
[root@unixmantra /]# tar xvf um-archive.tar   home/unixmantra/hellow.sh
home/unixmantra/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
[root@unixmantra /]#  tar xvf um-archive.tar home/unixmantra/tools/
home/unixmantra/tools/
home/unixmantra/tools/test1
home/unixmantra/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 unixmantra-backup.tar "filetest1" "filetest2" "filetest3"
# tar -zxvf unixmantra-backup.tar.gz "filetest1" "filetest2" "filetest3"
# tar -jxvf unixmantra-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
[root@unixmantra /]# tar rvf um-archive.tar testfile-new
testfile-new
[root@unixmantra /]# tar rvzf  um-archive.tar.gz testfile-new
Aborted
[root@unixmantra /]# 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 “Was shown below.
[root@unixmantra /]# tar tvfW um-archive.tar
tar: Skipping to next header
Verify -rw-r--r-- root/root         0 2013-09-22 21:50:26 home/unixmantra/tools/test1
Verify -rw-r--r-- root/root         8 2013-09-22 21:50:16 home/unixmantra/tools/perf-info.sh
Verify -rw-r--r-- root/root         5 2013-09-22 21:51:44 home/unixmantra/testfile1
Verify -rw-r--r-- root/root         6 2013-09-22 21:51:35 home/unixmantra/testfile2
Verify -rw-r--r-- root/root       656 2013-09-22 21:49:36 home/unixmantra/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
[root@unixmantra /]#tar --delete -f example.tar filename
# tar --delete -f um-archive.tar home/unixmantra/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).
[root@unixmantra /]# tar -cf - um-archive.tar | wc -l
15
[root@unixmantra /]# tar -czf - um-archive.tar.gz | wc -l
4
[root@unixmantra /]# 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" .


Let us know if you wish to include anymore "admin@unixmantra.com"

0 blogger-disqus:

Post a Comment