Use zgrep to grep a gzip (gz) file

Linux zgrep FAQ: How do I use the Linux zgrep command? (Or, How do I grep a GZ file?)

Linux users quickly learn how to use the Linux grep command on plain text files, but it takes a little longer to really you can grep gzip (gz) files as well. Here's how.

Problem - Want to grep gzip files

You want to grep a text file that has been compressed with gzip. You typically use the following steps to grep a gzip'd file, but you know there must be a better way:

gunzip myfile.gz
grep foo myfile
gzip myfile

Solution: the zgrep command

Unix and Linux systems come with a modified version of grep named zgrep. The Linux zgrep command works just like the grep command, except it works on text files that have been compressed with the gzip command.

This means that instead of following the three-step process shown above, you can just use zgrep to search a compressed text file in one step, like this:

zgrep foo myfile.gz

As another example, the zgrep command also works great on compressed Apache log files. For instance, if I want to see the hits on this blog in a gzip'd Apache log file, I'd use a zgrep command like this:

zgrep 'GET /blog' access_log.gz

or more likely I'd pipe the output into the Linux more command, like this:

zgrep 'GET /blog' access_log.gz | more

As you can see, using the zgrep command is much easier than using the three-step gunzip/grep gzip command I showed at the beginning of this article.


This tip is cool but don´t works for all plaforms.

-I tested on a Solaris 5.8, but didn´t find these file.
-I BSD, I also didn´t find it.
-On Linux, I found it.

Normaly I use this to perform the same:
%'tar .. | gzip .. | grep "patter"'.

Anyway, I like you trip (y)


Thanks for the info, that's good to know. I currently only have access to a few Linux systems and Mac OS X, and I know they're available on those platforms. I worked on an HP-UX system earlier this year, and these commands probably aren't available there either.
As I think about it, each of those systems probably has a package manager of some sort, so hopefully they can be added on that way, or possibly from a source distribution.
Thanks again!

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.