Monday, August 18, 2008

Compress/Uncompress Gzip, Zip, and Tar files

File Compression and Archiving

.bz2 — a file compressed with bzip2
---------------------------------------------

bzip2 and bunzip2 are file compression and decompression utilities. The bzip2 and bunzip2 utilities are newer than gzip and gunzip and are not as common yet, but they are rapidly gaining popularity. The bzip2 utility is capable of greater compression ratios than gzip. Therefore, a bzip2 file can be 10-20% smaller than a gzip version of the same file. Usually, files that have been compressed by bzip2 will have a .bz2 extension.

To compress a file using bzip2, execute the following command:
# bzip2 filename.txt (where filename.txt is the name of the file you wish to compress)

To uncompress a bzip2 file, execute the following command:
# bunzip2 filename.txt.bz2 (where filename.txt.bz2 is the name of the file you wish to uncompress)

The decompressed file from that of the compressed file as follows:

filename.bz2------becomes--filename
filename.bz -------becomes--filename
filename.tbz2 ---- becomes--filename.tar
filename.tbz ------becomes--filename.tar
anyothername --- becomes--anyothername.out

.gz — a file compressed with gzip
------------------------------------------

gzip and gunzip are GNU file compression and decompression utilities. Usually, files that have been compressed by gzip will have a .gz extension. However, sometimes you may see a file that has a .tgz extension. This is a TAR file that has been compressed by gzip. The .tgz extension is a shorthand version for the .tar.gz extension. This type of file must be uncompressed with gunzip before it can be untarred.

To compress a file using gzip, execute the following command:
# gzip filename.txt (where filename.txt is the name of the file you wish to compress)

To uncompress a gzip file, execute the following command:
# gunzip filename.txt.gz (where filename.txt.gz is the name of the file you wish to uncompress)

.tar.gz/.tgz/.tar.Z — a file compressed with Tar (Tape ARchive)
-------------------------------------------------------------------------------------

A file packaging tool included with UNIX/Linux for the purpose of assembling a collection of files into one combined file for easier archiving. It was originally designed for tape backup, but today can be used with other storage media. When run by itself, it produces files with a .tar extension. When combined with Gzip, for data compression, the resulting file extensions may be .tgz, .tar.gz or .tar.Z.

To compress a directory using tar, execute the following command:
# tar cvzf filename.tgz foldername (where foldername is the directory to be compressed and filename.tgz is the resultant compressed file)

c --- to create a tar file, writing the file starts at the beginning.
f --- specifies the filename (which follows the f) used to tar into or to tar out from.
z --- use zip/gzip to compress the tar file or to read from a compressed tar file.
v --- verbose output, show the files being stored into or restored from the tar file.

To see a tar file's table of contents use:
# tar tzf filename.tgz

t --- table of contents, see the names of all files or those specified in other command line arguments.

To uncompress a directory using tar, execute the following command:
# tar xvzf filename.tgz

x --- extract (restore) the contents of the tar file.

.zip — a file compressed with ZIP compression
---------------------------------------------------------------

To compress a file with zip, type the following:
zip -r filename.zip files (filename represents the file you are creating and files represents the files you want to put in the new file)

To extract the contents of a zip file, type:
unzip filename.zip

You can zip or gzip multiple files at the same time. List the files with a space between each one.

ref : http://www.redhat.com/docs/manuals/linux/RHL-7.2-Manual/getting-started-guide/s1-zip-tar.html
http://www.debianhelp.co.uk/bzandgzfiles.htm