LINUX CLASSES - COMPRESSION, ENCODING AND ENCRYPTION

Linux Gzip Command

Can I Compress a Linux File?

The gzip and gunzip Commands

The gzip program compresses a single file. One important thing to remember about gzip is that, unlike tar, it replaces your original file with a compressed version. (The amount of compression varies with the type of data, but a typical text file will be reduced by 70 to 80 percent.) Say you enter this command: gzip cheap.suit

You'll end up with a compressed file named cheap.suit.gz, and cheap.suit will be deleted.

To decompress the cheap.suit.gz file, enter this:

gunzip cheap.suit.gz

You'll get the original cheap.suit file back, and cheap.suit.gz will be deleted.

You can tell gzip to use different levels of compression with the -n flag, where n is a number from 1 to 9. The -1 flag means "fast but less efficient" compression, and -9 means "slow but most efficient" compression. Values of n between 1 and 9 will trade off speed and efficiency, and the default is -6. If you want to get the best possible compression and you don't mind waiting a little longer, use the -9 flag, like this:

gzip -9 cheap.suit

One other useful option is the -r flag, which tells gzip and gunzip to recursively compress or decompress all files in the current directory and any subdirectories. (Even with the -r flag, gzip still compresses one file at a time.) Here are some examples:

gzip -r somedir
gunzip -r somedir

The above commands will Zip or Unzip all files in the somedir directory.

Handling Compressed Archives

It's common to apply gzip to a tar file, which is why you see files with names like something.tar.gz on Linux systems. When you want to extract the contents of a gzipped tar file, you have several choices. The first is to use gunzip followed by tar, like this:

gunzip something.tar.gz
tar xvf something.tar

Or you could do it all in one command, like this:

gunzip -c something.tar.gz | tar xvf -

The -c flag tells gunzip to decompress the file, but instead of creating a something.tar file, it pipes the decompressed data directly to the tar command. The tar command on the right side of the pipeline looks a little strange, too--instead of a file name after the xvf, there's just a dash. The dash tells tar that the input is not an actual file on disk, but rather a stream of data from the pipeline. (Note that the gunzip input file is not deleted when you use the -c flag.)

Here's a third method of extracting the contents of a compressed tar file that's even easier. Remember the z flag with the tar command? You can use it to decompress and unbundle a tar file, like this:

tar xvzf something.tar.gz

The end result is exactly the same (the files that were in the compressed tar file are now in your current directory), but this is much easier than issuing multiple commands or writing a messy-looking gunzip-tar pipeline.

Note that this command will work on all Linux systems, but the z flag for tar is not always available on other flavors of Unix. (However, you can download and compile the source code for the GNU version of the tar command. See the note near the beginning of this section about getting the source code for the GNU utilities.)

For more information on the gzip command, see the gzip manual.

For more information on the gunzip command, see the gunzip manual.

Previous Lesson: Archiving With Tar
Next Lesson: Compress and Zcat

[ RETURN TO INDEX ]


   

Comments - most recent first
(Please feel free to answer questions posted by others!)

No comments yet

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 
 


Ask Bob Rankin - Free Tech Support


Copyright by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.