tar–Command Explained

tar–Command Explained

by -
0 790

Tar command is  archiving utility. The tar command bundles a bunch of files together and creates an archive on the External Media, Disk Drive. To create an archive using tar, use command like this, which bundles all the files in the current directory that end with .tar file.

The name tar stands for “Tape Archiver”  also known as  “Tarball”.

tar also supports compression,

No compression      :    .tar.
Gzip Compression  :    .tar.gz or .tgz
Bzip2 Compression:    .tar.bz2

Note: gzip compression is fast and less compressed in comparison with Bzip Compression, Bzip compression is slow but highly compressed.

Syntax

tar   option(s)   tar-filename  file-name(s)

These are the list of common options:

-c  option is used to create a new archive.
-v   verbosely list files which are processed.
-f   following is the archive file name.
-t   list the contents o fan archive.
-x   extract files from archive.
-z   filter the archive through  gzip.
-j    filter the archive through  bzip.
-C  directory file, performs a chdir operation on directory and performs the c (create) or r (replace operation on file.

The tar command is available by default on most linux systems and you do not need to install it separately.With tar there are two compression formats, gzip and bzip. The “z” option specifies gzip and “j” option specifies bzip. It is also possible to create uncompressed archives.”

Create tar Archive File Without Compression :

[root@localhost ~]# tar -cvf backup.tar /etc/sysconfig/*

tar: Removing leading `/’ from member names
/etc/sysconfig/atd
/etc/sysconfig/auditd
—————
—————
—————
/etc/sysconfig/smartmontools
/etc/sysconfig/sshd
/etc/sysconfig/sysstat
/etc/sysconfig/sysstat.ioconf
/etc/sysconfig/system-config-firewall
/etc/sysconfig/system-config-firewall.old
/etc/sysconfig/udev
[root@localhost ~]#

Incase of GZIP  means create backup in compress mode in .tar.gz form

[root@localhost ~]# tar -zcvf /data/backup.tar.gz /etc/sysconfig/*

In case of BZIP  means create backup high compression mode in .tar.bz  form

[root@localhost ~]# tar -jcvf /data/backup.tar.bz /etc/sysconfig/*

[root@localhost data]# ls -l
total 336
-rw-r–r–. 1 root root 245760 Jul 31 20:39 backup.tar         <———– See size without compression
-rw-r–r–. 1 root root 44852 Jul 31 20:40 backup.tar.bz      <———–See Size  with BZIP compression
-rw-r–r–. 1 root root 52583 Jul 31 20:40 backup.tar.gz       <———– See Size with GZIP compression
[root@localhost data]#

Extract tar file

[root@localhost ~]# mkdir /data
[root@localhost ~]# tar  -xvf   backup.tar   -C  /data/         <——extract tar file in /data folder
etc/sysconfig/atd
etc/sysconfig/auditd
etc/sysconfig/authconfig
————————-
————————-
————————-
etc/sysconfig/ntpd

[root@localhost ~]#

you can extract  .tar.bz and .tar.gz in the same way. You may use command either

[root@localhost ~]# tar  -zxvf   backup.tar.gz   -C  /data/   <———-untar  .tar.gz
[root@localhost ~]# tar  -jxvf   backup.tar.bz   -C  /data/   
<———-untar  .tar.bz

Listing an archive using tar command

[root@localhost ~]# tar  -tvf   backup.tar

Showing content of tar file without extracting the file.

Adding additional data  to an existing tarball.

[root@localhost ~]# tar -rvf backup.tar /etc/resolv.conf

This command insert additional data into existing tarball. here file /etc/resolv.conf will be inserted into tar file backup.tar. you can verify this using previous command.

This is very important before taking backup, We need to calculate required space using this command

Estimate the size of  tar file

[root@localhost ~]# tar -cf – /etc/sysconfig | wc -c
tar: Removing leading `/’ from member names
245760        <—————–this is the exactly same size of backup.tar

in case of  GZIP compression

[root@localhost ~]# tar -zcf – /etc/sysconfig | wc -c
tar: Removing leading `/’ from member names
52583            <————————- this is the size for backup.tar.gz
[root@localhost ~]#

In case of BZIP Compression

[root@localhost ~]# tar -jcf – /etc/sysconfig | wc -c
tar: Removing leading `/’ from member names
44852            <————————–this is the size for  backup.tar.bz
[root@localhost ~]#

Exclude Directories and Files

use an --exclude switch for each directory or file you want to exclude.

[root@localhost data]# tar -czvf sysbackup.tar.gz /etc/sysconfig –exclude=”/etc/sysconfig/network-scripts”

this command backup directory /etc/sysconfig but skips directory withing it i.e “/etc/sysconfig/network-scripts”

You can check it by command,should be no output

[root@localhost data]# tar -tvf sysbackup.tar.gz |grep  network-scripts        
[root@localhost data]#

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

we use option as W (verify), we cannot verify .tar.gz and .tar,bz file
[root@localhost data]# tar tvfW /data/backup.tar.bz
tar: This does not look like a tar archive
tar: Skipping to next header
tar: VERIFY FAILURE: 82 invalid headers detected
tar: Exiting with failure status due to previous errors
[root@localhost data]# tar tvfW /data/backup.tar
tar: This does not look like a tar archive
tar: Skipping to next header
Verify ———- 0/0             398 1970-01-01 05:30 etc/sysconfig/authconfig
tar: etc/sysconfig/authconfig: Warning: Cannot stat: No such file or directory
Verify ———- 0/0            4091 1970-01-01 05:30 etc/sysconfig/autofs
tar: etc/sysconfig/autofs: Warning: Cannot stat: No such file or directory
Verify d——— 0/0               0 1970-01-01 05:30 etc/sysconfig/cbq/
tar: etc/sysconfig/cbq: Warning: Cannot stat: No such file or directory
Verify ———- 0/0              11 1970-01-01 05:30 etc/sysconfig/cbq/avpkt
tar: etc/sysconfig/cbq/avpkt: Warning: Cannot stat: No such file or directory
Verify ———- 0/0              79 1970-01-01 05:30 etc/sysconfig/cbq/cbq-0000.example
tar: etc/sysconfig/cbq/cbq-0000.example: Warning: Cannot stat: No such file or directory
Verify ———- 0/0             486 1970-01-01 05:30 etc/sysconfig/cgconfig
tar: etc/sysconfig/cgconfig: Warning: Cannot stat: No such file or directory
Verify ———- 0/0             950 1970-01-01 05:30 etc/sysconfig/cgred.conf
tar: etc/sysconfig/cgred.conf: Warning: Cannot stat: No such file or directory

Note: tar command also works if switches are given without  –  symbol. Tar command preserves ownership and permission of the file and directories

tar cvf  backup.tar file1    and    tar cvf backup.tar file1   is same

 

Thanks !!  Enjoy Linux ——————–!!!

CEO, KV IT-Solutions Pvt. Ltd. | vikas@kvit.in | 9810028374|
Linux Professional and an Industrial Trainer | 20 + years Experience in IT Industry

” We are born free, No Gate and Windows can snatch our freedom “

Download PDF

SIMILAR ARTICLES

0 1123

0 749

1 758

NO COMMENTS

Leave a Reply

Required Captcha *