Linux Classes
Share This With a Friend  

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



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

Fernando Melo Medeiros     (06 Jan 2015, 12:50)
Bob, I would appreciate it if you could help me to install MediaDrug in my Ubuntu 14.04.1 LTS (Trusty Tahr).
Fernando     (16 Jan 2014, 14:50)
Hi, Bob,
Following what I've been doing in order to install MediaDrug in my machine through the file "mediadrug.tar.gz", I've just used "tar xvfa mediadrug.tar.gz". As a result I got the file "MediaDrug-Linux-x86-Install". Now please what do I do? Because up to now, when I've neede to installl some program in my machine I usually use "apt-get install", so I really don't know how I have to proceed to have MediaDrug installed just using only "install".

I would appreciate your competent help to help me to solve this problem of mine.
Fernando     (16 Jan 2014, 06:37)
I have read the above comments and tried all of them but I haven't succeeded in unzipping "mediadrug.tar.gz". All the replies I get says that "mediadrug.tar.gz" is not in gzip format.
Fernando Melo Medeiros     (21 Oct 2013, 15:53)
Dear Sir,

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.
OT     (10 Feb 2012, 03:08)
Thanks for the great site. Where can I send my money ?
Bas     (27 Oct 2011, 09:14)
I have to apologize Bob, I was the one being stupid! I tried before reading.. excuse me!
Ice     (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".
William     (04 Sep 2011, 07:59)
Very nice, i suggest webmaster can set up a forum, so that we can talk and communicate.
gootooyoo     (04 Aug 2011, 22:47)
i want to zip in folder with "-r" but not work.
Bob Rankin     (14 Jul 2011, 11:56)
@Bas - Which example do you think is wrong?
Bas     (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!
Ganesan     (25 Feb 2011, 03:03)
Try this command.
gunzip -c figlet222.tar.gz
MaxTheDork     (23 Feb 2011, 14:56)

gunzip figlet222.tar.gz


tar -xvf figlet222.tar
Debulet     (13 Feb 2011, 22:47)
how can i extract the source for a file
file name is figlet222.tar.gz
Chris     (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.
jd     (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.
CJL     (14 Jul 2010, 11:09)
Nice concise article, told me exactly what I needed to know... thanks!
Bob Rankin     (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!
spunky     (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.
Notify me about new comments on this page
Hide my email

Ask Bob Rankin - Free Tech Support

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