Grep compressed (.gz) files at a shell prompt

by on March 29, 2007 · 0 comments· last updated at January 29, 2008

Q. I've lots of .gz files in my directory and I'd like to search them using grep. Unfortunately, grep is not working on my compressed file How do I write a script to automate entire procedure to grep some compressed files. Do I have to uncompress them first and run grep over them?

A. No you don't have to uncompress file. Linux and UNIX comes with z-commands. zgrep invokes grep on compressed or gzipped files. All options specified are passed directly to grep.

To search .gz file called test.gz, enter:
$ zgrep 'word-to-search' /path/to/test.gz
$ zgrep 'GET /faq/a-long-url/' /path/to/access.log.gz

You can run zcat command to display file on screen:
$ zcat file.gz

Update: See list of all z-commands



You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as: , ,

Previous Faq:

Next Faq: