OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

gzip

The gzip program is used to compress one or more files. When executed, it replaces the original file with a compressed version of the original. The corresponding gunzip pro- gram is used to restore compressed files to their original, uncompressed form. Here is an example:



[me@linuxbox ~]$ ls -l /etc > foo.txt

[me@linuxbox ~]$ ls -l foo.*

-rw-r--r-- 1 me me 15738 2016-10-14 07:15 foo.txt [me@linuxbox ~]$ gzip foo.txt

[me@linuxbox ~]$ ls -l foo.*

-rw-r--r-- 1 me me 3230 2016-10-14 07:15 foo.txt.gz [me@linuxbox ~]$ gunzip foo.txt

[me@linuxbox ~]$ ls -l foo.*

-rw-r--r-- 1 me me 15738 2016-10-14 07:15 foo.txt

[me@linuxbox ~]$ ls -l /etc > foo.txt

[me@linuxbox ~]$ ls -l foo.*

-rw-r--r-- 1 me me 15738 2016-10-14 07:15 foo.txt [me@linuxbox ~]$ gzip foo.txt

[me@linuxbox ~]$ ls -l foo.*

-rw-r--r-- 1 me me 3230 2016-10-14 07:15 foo.txt.gz [me@linuxbox ~]$ gunzip foo.txt

[me@linuxbox ~]$ ls -l foo.*

-rw-r--r-- 1 me me 15738 2016-10-14 07:15 foo.txt


In this example, we create a text file named foo.txt from a directory listing. Next, we run gzip, which replaces the original file with a compressed version named foo.tx- t.gz. In the directory listing of foo.*, we see that the original file has been replaced with the compressed version, and that the compressed version is about one-fifth the size of the original. We can also see that the compressed file has the same permissions and timestamp as the original.

Next, we run the gunzip program to uncompress the file. Afterward, we can see that the compressed version of the file has been replaced with the original, again with the permis-


sions and timestamp preserved.

gzip has many options. Here are a few:


Table 18-1: gzip Options


Option Description

Option Description

-c Write output to standard output and keep original files. May also be specified with --stdout and --to-stdout.


image

-d Decompress. This causes gzip to act like gunzip. May also be specified with --decompress or --uncompress.


image

-f Force compression even if a compressed version of the original file already exists. May also be specified with --force.


image

-h Display usage information. May also be specified with --help.


image

-l List compression statistics for each file compressed. May also be specified with --list.


image

-r If one or more arguments on the command line are directories, recursively compress files contained within them. May also be specified with --recursive.


image

-t Test the integrity of a compressed file. May also be specified with

--test.


image

-v Display verbose messages while compressing. May also be specified with --verbose.


image

-number Set amount of compression. number is an integer in the range of 1 (fastest, least compression) to 9 (slowest, most compression). The values 1 and 9 may also be expressed as --fast and --best, respectively. The default value is 6.


image


Going back to our earlier example:



[me@linuxbox ~]$ gzip foo.txt [me@linuxbox ~]$ gzip -tv foo.txt.gz foo.txt.gz: OK

[me@linuxbox ~]$ gzip -d foo.txt.gz

[me@linuxbox ~]$ gzip foo.txt [me@linuxbox ~]$ gzip -tv foo.txt.gz foo.txt.gz: OK

[me@linuxbox ~]$ gzip -d foo.txt.gz


Here, we replaced the file foo.txt with a compressed version named foo.txt.gz. Next, we tested the integrity of the compressed version, using the -t and -v options. Fi-


nally, we decompressed the file back to its original form.

gzip can also be used in interesting ways via standard input and output:


[me@linuxbox ~]$ ls -l /etc | gzip > foo.txt.gz

[me@linuxbox ~]$ ls -l /etc | gzip > foo.txt.gz


This command creates a compressed version of a directory listing.

The gunzip program, which uncompresses gzip files, assumes that filenames end in the extension .gz, so it’s not necessary to specify it, as long as the specified name is not in conflict with an existing uncompressed file:



[me@linuxbox ~]$ gunzip foo.txt

[me@linuxbox ~]$ gunzip foo.txt


If our goal were only to view the contents of a compressed text file, we could do this:



[me@linuxbox ~]$ gunzip -c foo.txt | less

[me@linuxbox ~]$ gunzip -c foo.txt | less


Alternately, there is a program supplied with gzip, called zcat, that is equivalent to gunzip with the -c option. It can be used like the cat command on gzip compressed files:



[me@linuxbox ~]$ zcat foo.txt.gz | less

[me@linuxbox ~]$ zcat foo.txt.gz | less


image

Tip: There is a zless program, too. It performs the same function as the pipeline above.


image


Top OS Cloud Computing at OnWorks: