Wednesday, January 24, 2018

Tar Command Examples in Linux - Part(2)

11. Untar Single file from tar.gz File

To extract a single file manishbackup.xml from manishbackup.tar.gz archive file, use the command as follows.

# tar -zxvf manishbackup.tar.gz manishbackup.xml
OR
# tar --extract --file=manishbackup.tar.gz manishbackup.xml
manishbackup.xml


12. Untar Single file from tar.bz2 File

To extract a single file called index.php from the file Phpfiles-org.tar.bz2 use the following option.

# tar -jxvf Phpfiles-org.tar.bz2 home/php/index.php
OR
# tar --extract --file=Phpfiles-org.tar.bz2 /home/php/index.php
/home/php/index.php


13. Untar Multiple files from tar, tar.gz and tar.bz2 File

To extract or untar multiple files from the tartar.gz and tar.bz2 archive file. For example the below command will extract “file 1” “file 2” from the archive files.

# tar -xvf manish-14-09-12.tar "file 1" "file 2" 
# tar -zxvf MyImages-14-09-12.tar.gz "file 1" "file 2" 
# tar -jxvf Phpfiles-org.tar.bz2 "file 1" "file 2"


14. Extract Group of Files using Wildcard

To extract a group of files we use wildcard based extracting. For example, to extract a group of all files whose pattern begins with .php from a tar, tar.gz and tar.bz2 archive file.

# tar -xvf Phpfiles-org.tar --wildcards '*.php'
# tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php'
# tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php'
/home/php/iframe_ew.php
/home/php/videos_all.php
/home/php/rss.php
/home/php/index.php
/home/php/vendor.php
/home/php/video_title.php
/home/php/report.php
/home/php/video.php


15. Add Files or Directories to tar Archive File

To add files or directories to existing tar archived file we use the option r (append). For example we add file xyz.txt and directory php to existing manish-14-09-12.tar archive file.

# tar -rvf manish-14-09-12.tar xyz.txt
# tar -rvf manish-14-09-12.tar php
drwxr-xr-x root/root         0 2012-09-15 02:24:21 home/manish/
-rw-r--r-- root/root  15740615 2012-09-15 02:23:42 home/manish/cleanfiles.sh
-rw-r--r-- root/root    863726 2012-09-15 02:23:41 home/manish/openvpn-2.1.4.tar.gz
-rw-r--r-- root/root  21063680 2012-09-15 02:24:21 home/manish/manish-14-09-12.tar
-rw-r--r-- root/root   4437600 2012-09-15 02:23:41 home/manish/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
-rw-r--r-- root/root     12680 2012-09-15 02:23:41 home/manish/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
-rw-r--r-- root/root 0 2012-08-18 19:11:04 xyz.txt
drwxr-xr-x root/root 0 2012-09-15 03:06:08 php/ 
-rw-r--r-- root/root 1751 2012-09-15 03:06:08 php/iframe_ew.php 
-rw-r--r-- root/root 11220 2012-09-15 03:06:08 php/videos_all.php 
-rw-r--r-- root/root 2152 2012-09-15 03:06:08 php/rss.php 
-rw-r--r-- root/root 3021 2012-09-15 03:06:08 php/index.php 
-rw-r--r-- root/root 2554 2012-09-15 03:06:08 php/vendor.php 
-rw-r--r-- root/root 406 2012-09-15 03:06:08 php/video_title.php


16. Add Files or Directories to tar.gz and tar.bz2 files

The tar command don’t have a option to add files or directories to an existing compressed tar.gz and tar.bz2archive file. If we do try will get the following error.

# tar -rvf MyImages-14-09-12.tar.gz xyz.txt
# tar -rvf Phpfiles-org.tar.bz2 xyz.txt
tar: This does not look like a tar archive
tar: Skipping to next header
xyz.txt
tar: Error exit delayed from previous errors


17. How To Verify tar, tar.gz and tar.bz2 Archive File

To verfify any tar or compressed archived file we use option as W (verify). To do, just use the following examples of command. (Note : You cannot do verification on a compressed ( *.tar.gz, *.tar.bz2 ) archive file).

# tar tvfW manish-14-09-12.tar
tar: This does not look like a tar archive
tar: Skipping to next header
tar: Archive contains obsolescent base-64 headers
tar: VERIFY FAILURE: 30740 invalid headers detected
Verify -rw-r--r-- root/root    863726 2012-09-15 02:23:41 /home/manish/openvpn-2.1.4.tar.gz
Verify -rw-r--r-- root/root  21063680 2012-09-15 02:24:21 /home/manish/manish-14-09-12.tar
tar: /home/manish/manish-14-09-12.tar: Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root   4437600 2012-09-15 02:23:41 home/manish/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
tar: /home/manish/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm: Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root     12680 2012-09-15 02:23:41 home/manish/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
tar: /home/manish/rpmforge-release-0.5.2-2.el5.rf.i386.rpm: Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root         0 2012-08-18 19:11:04 xyz.txt
Verify drwxr-xr-x root/root         0 2012-09-15 03:06:08 php/


18. Check the Size of the tar, tar.gz and tar.bz2 Archive File

To check the size of any tartar.gz and tar.bz2 archive file, use the following command. For example the below command will display the size of archive file in Kilobytes (KB).

# tar -czf - manish-14-09-12.tar | wc -c
12820480
# tar -czf - MyImages-14-09-12.tar.gz | wc -c
112640
# tar -czf - Phpfiles-org.tar.bz2 | wc -c
20480


Tar Usage and Options

1.     c – create a archive file.
2.     x – extract a archive file.
3.     v – show the progress of archive file.
4.     f – filename of archive file.
5.     t – viewing content of archive file.
6.     j – filter archive through bzip2.
7.     z – filter archive through gzip.
8.     r – append or update files or directories to existing archive file.
9.     W – Verify a archive file.
10. wildcards – Specify patterns in unix tar command.


That’s it for now, hope the above tar command examples are enough for you to learn and for more information please use man tar command.

No comments:

Post a Comment

Configure NFS Client

This example is based on the environment below. +----------------------+           |           +----------------------+ | [...