Compressing Files

Two compression programs are in common use on Linux, gzip and bzip2; gzip is more common, but in general, bzip2 leads to somewhat smaller file sizes. To compress a file using gzip, do this:

[email protected]:~/temp> gzip afile [email protected]:~/temp> ls afile.gz

The file afile has been compressed to the file afile.gz. To restore the original file you can use the gunzip command (or the equivalent gzip -d).

[email protected]:~/temp> gunzip afile.gz [email protected]:~/temp> ls afile

Notice how each time the original is no longer there. If you want to keep the original file while writing the compressed file, you can use the -c option (which writes the output to standard output) and redirect that output to a file.

By default, gzip keeps the original permissions and timestamp and restores them on decompression. With the -N option, the name is also preserved, even if you change the name of the compressed file.

The bzip2 program behaves in a very similar way to gzip:

[email protected]:~/temp> bzip2 afile [email protected]:~/temp> ls afile.bz2

To uncompress, you can use bunzip2 or the equivalent bzip2 -d:

[email protected]:~/temp> bunzip2 afile.bz2

[email protected]:~/temp> ls afile

It is very common to see gzip and bzip2 being used together with tar, which is discussed later in the chapter.

One or two applications are capable of reading in files in the gzip format and uncompressing them and reading them on the fly. In particular, if you come across .ps.gz files (gzipped PostScript files), the programs gv and kghostview can read these "as is" without first decompressing them.

Also, a number of standard utilities have versions that first uncompress the file (assuming it to be gzipped). By convention, a letter z at the start of the name indicates this. For example:

♦ zgrep first uncompresses the file and then runs the grep command.

♦ zless and zmore are versions of less and more that first uncompress the file they are

Chapter 2 contains more information on the commands grep, 1 ess, and more.

acting on.

Was this article helpful?

0 0

Post a comment