Saturday, 28 January 2012

gzip and tar

gzipis used to compress files and taris used to archive a file system. Though these two are different commands ,using them together canaccomplish a variety of tasks which seem to be

much simplerthanwhenrunning them separately. Here we will also discuss about the combineduse 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.

gzip

Example 1)

gzipis a compression utility that uses Huffmancoding to compress files and save them in .gz format . The.gzfile occupieslesser space thanthe original file. We will not discuss about the .gz file format , but only show how the commandcan be used along with its options.

To compress a filestake-holder.csv ,

/home/gr-adm $ ls –lrtstake-holder.csv*

-rw-r--r--1 gr-admAdminist56344 Dec 16 14:37 greenlife.txt

/home/gr-adm$ gzipfilestake-holder.csv

/home/gr-adm$ l s-lrtstake-holder.csv*

-rw-r--r--1 kaushikAdminist20758 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.

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

Here , Old-stake-holder.csv.gz can be a pre existing .gz file or a new filewith 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.gzandplaces the original file

stake-holder.csv in /backup/stakes/ itself.

Example 4)

Toview the contents of the file withoutcompletely decompressing the file, use gzip –dc.

Use pg command to view it pagewise..

/home/gr-adm$ gzip-dcstake-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 ofthefile stake-holder.csv.gz,

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

tar.

tar is a short form for tape archive. Itcancreatea single file withthe contents of a directory structurein one pathor extract afile into directory structure in another path.

Example1)

Tocreatea tar file of a directory structure and to view the progress of taruse,

/home/gr-adm$ tar-cvf/archives/archive2/data-history.tardata/notes

a data/notes/Nov-10/exceptions.txt200 bytes

a data/notes/Nov-12/promotions.csv123 bytes

a data/notes/N0v-13/oracle-log/ora-errors.log456bytes

a data/notes/tea-time.in2076 bytes

a data/notes/network/virtual-ips.bmp1290bytes

a data/notes/levy.list87bytes

/home/gr-adm $

This creates an archive filedata-history.tarin the path /archives/archive2which contains the directory/home/notes and all itsfiles and directory structure.

Example2)

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

/home/gr-adm$ cd/base-app/archive

/home/gr-adm$tar-xvf/archives/archive2/data-history.tar

xdata/notes/Nov-10/exceptions.txt200 bytes

xdata/notes/Nov-12/promotions.csv123 bytes

xdata/notes/N0v-13/oracle-log/ora-errors.log456bytes

xdata/notes/tea-time.in

xdata/notes/network/virtual-ips.bmp

xdata/notes/levy.list

/home/gr-adm $

Example3)

Insteadofgivingentire directory nameasa parameter to create a tar file, a list of files whichcare 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$ catto-tar-list

data/notes/Nov-10/exceptions.txt

data/notes/N0v-13/oracle-log/ora-errors.log

data/notes/levy.list

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