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:
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:
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!)
Fernando Melo Medeiros
(21 Oct 2013, 15:53
I have a problem.I have been trying to install "MediaDrug-Linux-x86-Install.tar.gz", but my system, a 64 bit, will not accept it. Instead I get a reply saying this is not a gzip file. I don't know what to do as my knowledge of the Linux system is not good enough despite of
the 10 years I have been trying to understand it. I would appreciate if you could help me as I have already thought of throwing my computer through the window from the seventh floor of the building where I live on the street.
(10 Feb 2012, 03:08
Thanks for the great site. Where can I send my money ?
(27 Oct 2011, 09:14
I have to apologize Bob, I was the one being stupid! I tried before reading.. excuse me!
(22 Sep 2011, 05:06
Nice, thanks for the concise reference page! Got exactly what was looking for from the first google search. :)
As a side note to other commenters: "-r" is a switch for recursively traversing directories and packing or unpacking each of the encountered files. If you want to pack a whole
directory, as the page says, you need to tar it first. Or if you're lazy, use "tar -czf tarball.tar.gz directory_name" to create a gzipped tarball without any piping needed. Matching command for extracting everything from that file would be "tar -xf filename.tar.gz".
(04 Sep 2011, 07:59
Very nice, i suggest webmaster can set up a forum, so that we can talk and communicate.
(04 Aug 2011, 22:47
i want to zip in folder with "-r" but not work.
(14 Jul 2011, 11:56
@Bas - Which example do you think is wrong?
(14 Jul 2011, 09:14
You are pretty stupid. Quickly searching for a gzip command to zip up my www dir of a live website I managed to completely destroy a live site using your (incorrect) gzip example.
If you don't know, please don't post. Idiot!
(25 Feb 2011, 03:03
Try this command.
gunzip -c figlet222.tar.gz
(23 Feb 2011, 14:56
tar -xvf figlet222.tar
(13 Feb 2011, 22:47
how can i extract the source for a file
file name is figlet222.tar.gz
(27 Jul 2010, 05:37
The instructions are valid.
He just forgot the comment so it should be like:
gzip -r somedir #Zip all files in the somedir directory.
gunzip -r somedir #Unzip all files in the somedir directory.
(15 Jul 2010, 07:44
Thanks Dr Bob.
@spunky. Post up a link to your site so that we may all look at your wonderful helpguides.
(14 Jul 2010, 11:09
Nice concise article, told me exactly what I needed to know... thanks!
(13 Jul 2010, 20:18
Sorry, you're wrong. The -r flag DOES act on the target directory, to either zip or unzip all files in that directory, one at a time. I just created a test directory and verified -- try it yourself!
(13 Jul 2010, 16:39
Your instructions are terrible. Your examples are not valid examples actually!
gzip -r somedir Zip all files in the somedir directory.
gunzip -r somedir Unzip all files in the somedir directory.
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.
Copyright © by Bob Rankin