Compression in Linux

30 01 2010

To deflate files in Linux, gzip and zip commands are commonly used. The difference between these 2 commands are

  1. gzip removes the original source file while zip retains the source file after compressing.
  2. gzip compresses each file individually, but zip compacts the files into one archive file.

To illustrate,

[ambient@localhost box]$ ls -l
total 24
-rwxr-xr-x 1 ambient ambient 11816 Dec 31 07:39 htpasswd
-rw-r--r-- 1 ambient ambient    60 Dec 31 07:39 udb1
[ambient@localhost box]$ gzip *
[ambient@localhost box]$ ls -l
total 20
-rwxr-xr-x 1 ambient ambient 5667 Dec 31 07:39 htpasswd.gz
-rw-r--r-- 1 ambient ambient   80 Dec 31 07:39 udb1.gz
[ambient@localhost box]$ gunzip *.gz
[ambient@localhost box]$ ls -l
total 24
-rwxr-xr-x 1 ambient ambient 11816 Dec 31 07:39 htpasswd
-rw-r--r-- 1 ambient ambient    60 Dec 31 07:39 udb1
[ambient@localhost box]$ zip pack.zip *
adding: htpasswd (deflated 52%)
adding: udb1 (deflated 5%)
[ambient@localhost box]$ ls -l
total 36
-rwxr-xr-x 1 ambient ambient 11816 Dec 31 07:39 htpasswd
-rw-r--r-- 1 ambient ambient  5963 Dec 31 08:15 pack.zip
-rw-r--r-- 1 ambient ambient    60 Dec 31 07:39 udb1
[ambient@localhost box]$ unzip pack.zip
Archive:  pack.zip
replace htpasswd? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace udb1? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
[ambient@localhost box]$ ls -l
total 36
-rwxr-xr-x 1 ambient ambient 11816 Dec 31 07:39 htpasswd
-rw-r--r-- 1 ambient ambient  5963 Dec 31 08:15 pack.zip
-rw-r--r-- 1 ambient ambient    60 Dec 31 07:39 udb1

In brief,


> gzip srcfile //deflate
> gunzip srcfile.gz //inflate
#############
> zip file.zip srcfile //deflate
> unzip file.zip //inflate

tar

In the past, tar command was used to create an archive file on tapes, so it is called as “Tape ARchive”.

To bundle files into an archive
> tar -cvf pack.tar *

To list
> tar -tvf pack.tar

And to extract the archive
> tar -xvf pack.tar

Make an archive before compressing files is a convention way to distribute source or binary files in Linux.


Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: