User Tools

Site Tools


linux:grep-compressed_files

Grep compressed (.gz) files at a shell prompt

by nixCraft on March 29, 2007 - 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

linux/grep-compressed_files.txt · Last modified: 2013-03-07 08:16 by kokkez