CLI and Compression ToolsAny computer user, no matter what his/her os background, knows the importance of data compression in terms of saving disk space and backing up files. Heres a look at some of the ways of doing it using a few open source compression tools, from the command line interface. GNU Zipgzip(1) is a very well known file compression/decompression utility in the Unix or rather the `Unix-like' world. It is based on Lempel-Ziv (LZ77) and Huffman coding. The easiest way of using gzip to compress a file named, say, `foo.x' is -
$ gzip foo.x
which will replace foo.x with foo.x.gz. In case you want gzip to leave the original files untouched, you can use the -c option to send the compressed file to stdout so you may subsequently redirect it to another file, say, new_foo.gz.
$ gzip -c foo.x > my_foo.gz
gzip on its own is only useful when you want to compress single files. But when used with the Tar utility, it becomes an even more powerful tool useful for creating compressed archives. Tar and GzipThe idea of using tar(1) and gzip together is to create a tarball of the set of files to be archived and then use gzip to compress it producing a .tar.gz file. The following commands generate a tarball, say foos.tar, out of two files, say foo1.x and foo2.x.
$ tar -c foo1.x foo2.x -f foos.tar
$ tar -c foo1.x foo2.x -f - > foos.tar
Both the examples shown above do the same. The -c option tells the tar program to create an archive whereas the -f option specifies the file name of the tarball to be created. The `-' in the second example specifies the output to be written to the stdout. To archive a complete directory, say foo_dir while maintaining the directory structure -
$ tar -c foo_dir -f foo_dir.tar
$ tar -c foo_dir -f - > foo_dir.tar
Again, both the examples do the same thing in two different ways. Once the tarball has been created, it can be compressed using gzip.
$ gzip -c foo_dir.tar > foo_dir.tar.gz
which will create food_dir.tar.gz which is the gzip'ed tarball of the directory foo_dir. Now that we know the basic steps, lets do it the unix way using pipes -
$ tar -c foo_dir -f - | gzip - -c > foo_dir.tar.gz
BZip2bzip2(1) compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. Compression is generally better than conventional LZ77 based utilities such as gzip. The usage and the command line interace of bzip2 is quite similar to gzip. For example -
$ bzip2 foo1.x
will compress foo1.x and replace it with foo1.x.bz2. To create bzip2 compressed archives, you can use tar in exactly the same way as described above with gzip. ZipThe zip(1) utility is a file compression and packaging utility, compatbile with PKZIP. It is analogous to using tar and gzip to produce a compressed archive of files. The following is the simplest example of producing zip package of a directory, say foo_dir -
$ zip -r foo_dir foo_dir
which will produce foo_dir.zip.
|
TopicsRecent blog posts
|