Saturday, 28 January 2012

gzip and tar

 gzip and tar
gzip  is used to compress files and tar  is used to archive a file system. Though these two are different commands ,using them together can  accomplish a variety of tasks which seem to be
much simpler  than  when  running them separately. Here we will also discuss about the combined  use of tar and cd commands.

First we will discuss with examples the individual command usages of gzip and tar and later use them in combination.

Example 1)
gzip  is a compression utility that uses Huffman  coding to compress files and save them in .gz format . The  .gz  file occupies  lesser space than   the original file. We will not discuss about the .gz file format , but only show how the command  can be used along with its options.

To compress a file  stake-holder.csv , 
/home/gr-adm $ ls –lrt  stake-holder.csv*
-rw-r--r--    1 gr-adm  Administ    56344 Dec 16 14:37 greenlife.txt

/home/gr-adm$ gzip  file  stake-holder.csv
/home/gr-adm$ l s   -lrt  stake-holder.csv*
-rw-r--r--    1 kaushik  Administ    20758 Dec 16 14:37 greenlife.txt.gz

Example 2)
If you want to retain the original file even after compression or want the .gz file to have a different name, use gzip –c.

To  compress the file stake-holder.csv and save the  .gz file in some other directory,

/home/gr-adm$ gzip  -c  stake-holder.csv   >/backup/stakes/ stake-holder.csv.gz.

 You can also append the compressed file into an existing  .gz file.
gzip  -c  stake-holder.csv    >>/backup/stakes/Old-stake-holder.csv.gz

Here , Old-stake-holder.csv.gz can be a pre existing .gz file or a new file  with that name will be created.

Example 3)
To decompress the .gz file use gunzip  <filename.gz>,
/home/gr-adm$ gunzip    /backup/stakes/stake-holder.csv.gz
This command will decompress the file stake-holder.csv.gz  and  places the original file  
stake-holder.csv in /backup/stakes/ itself.

Example 4)
To  view the contents of the file without  completely decompressing the file  , use gzip –dc.
Use pg command to view it pagewise..
/home/gr-adm$ gzip  -dc  stake-holder.csv.gz  |  pg
A premji, wipro,67
N R murthy,Infosys,65
N Chandra,TCS,46
You can redirect the output into another file,
For eg to redirect first 5 lines of  the  file stake-holder.csv.gz   ,

gzip  –dc  stake-holder.csv.gz   | head  -5  >stake-holder-five.csv.


tar is a short form for tape archive. It  can  create  a single file with  the contents of a directory structure   in one path  or extract a  file into directory structure in another path.

To  create  a tar file of a directory structure and to view the progress of tar  use,

/home/gr-adm$ tar  -cvf   /archives/archive2/data-history.tar    data/notes
a data/notes/Nov-10/exceptions.txt    200 bytes
a data/notes/Nov-12/promotions.csv     123 bytes
a data/notes/N0v-13/oracle-log/ora-errors.log    456  bytes
a data/notes/     2076 bytes
a data/notes/network/virtual-ips.bmp    1290bytes
a data/notes/levy.list   87bytes
/home/gr-adm $

This creates an archive file  data-history.tar    in the path /archives/archive2   which contains the directory  /home/notes and all its  files and directory structure.

To extract the contents of the archive file into a different location , first change your working directory to the location where you  wish to extract the file, then extract and view the progress by running tar  with –xvf  switch.

/home/gr-adm$ cd   /base-app/archive
/home/gr-adm$   tar   -xvf   /archives/archive2/data-history.tar   
x  data/notes/Nov-10/exceptions.txt    200  bytes
x  data/notes/Nov-12/promotions.csv     123 bytes
x  data/notes/N0v-13/oracle-log/ora-errors.log    456  bytes
x  data/notes/
x  data/notes/network/virtual-ips.bmp
x  data/notes/levy.list
/home/gr-adm $

Instead  of   giving  entire directory name  as  a parameter to create a tar file, a   list of files which  care to be archived can be can be passed to tar with –L option.
A file containing the list of files to take tar is as shown.
/home/gr-adm$ cat  to-tar-list

Now to create an archive of only these specific files ,use tar in the following manner.

/home/gr-adm$ tar  -cvf   /archives/archive2/data-specific.tar     -L   to-tar-list
a  data/notes/Nov-10/exceptions.txt      200 bytes
a  data/notes/N0v-13/oracle-log/ora-errors.log           456 bytes
a  data/notes/levy.list    87 bytes

Sometimes  you  may need to exclude  files from  the tar archive. To do this place the files  and directories to be excluded in a list file and then use tar –X  <exclude-list>.

/home/gr-adm$ cat  /home/fiuser/excl-list

/home/gr-adm$ tar  -X  /home/fiuser/excl-list   -cvf  /archives/data-specific2 .tar    data/notes
a data/notes/     2076 bytes
a data/notes/network/virtual-ips.bmp    1290bytes
a data/notes/levy.list   87bytes

The  contents of the tar file cannot be viewed directly by cat or more , but  to view the details of the  files and directories contained in the tar file ,use tar –tvf

/home/gr-adm$ tar  -tvf   /archives/data-specific2 .tar
-rw-r--r--       gr-adm      2076        2011-12-16   15:52:55     data/notes/    
-rw-r--r--       gr-adm      1290        2011-12-30   22:44:59    data/notes/network/virtual-ips.bmp   
-rw-r--r--       gr-adm       87             2011-12-30   22:45:48    data/notes/levy.list    

Now that we have gone through tar and gzip individually in detail, let us discuss  with examples  the  use of combined forces of  tar and cd command.

·      to take the tar of  data/notes  directory and  untar   the contents  in  /archives/archive2

tar  cvf   data/notes | (cd  /archives/archive2;tar xvf  - )

‘ –‘   refers to the  console .so it means you are passing the output of  tar  -cvf  to   tar –xvf  and changing your path to  /archives/archive2.

·   to take the tar of  local directory data/notes  and untar the contents in  a directory /archives/archive2   of  remote machine

tar  cvf  -  data/notes | rsh  -l  remuser  "cd /archives/archive2;tar xvf -;"

·     to take the tar of files in list file tar_list_1 and untar in /archives/archive2

             tar  cvf  - `cat tar_list_1` | (cd   /archives/archive2;tar xvf  - )

·       to take the tar of files in a local  list file tar_list_1 and untar in /archives/archive2 of  a            remote machine

tar cvf - `cat tar_list_1` | rsh - l remuser  "cd /archives/archive2;tar xvf  -"

·       to take the tar of data/notes directory excluding those in excl_list_tar  and untar its contents     in /archives/archive2

                 tar   –X   excl_list_tar   cvf - data/notes | (cd   /archives/archive2;tar xvf -)

Finally we will discuss with examples   the combined use of gzip and tar.

·      to gunzip a zipped tar file  data_archive.tar.gz and untar It in /archives/archive2 .

gzip  -dc  data_archive.tar.gz  - | (cd /archives/archive2;tar xvf  -)

·      to gunzip a local  zipped tar  file  data_archive.tar.gz  and untar it in /archives/archive2   of      a  remote machine 
 gzip -dc data_archive.tar.gz  - | rsh -l  fnsonlad  "cd /archives/archive2;tar  xvf  –“

·      To  view the contents of a zipped tar file without  unzipping it, page wise

       gzip -dc data_archive.tar.gz  - |  tar  tvf  -  |pg

No comments:

Post a Comment