Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I have a set of 100 log files, compressed using gzip. I need to find all lines matching a given expression. I'd use grep, but of course, that's a bit of a nightmare because I'll have to unzip all files, one by one, grep them and delete the unzipped version, because they wouldn't all fit on my sevrer if they were all unzipped.

Anyone has a little trick on how to get that done quickly?

share|improve this question

marked as duplicate by Iain, MadHatter, MikeyB, DerfK, 84104 Oct 21 '13 at 21:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

You mentioned the requirement of needing to find all lines matching a given expression. Does finding them mean that you need to know which file they're in? If not, more options are open to you, particularly those that pipe their output to the stdin of grep. – LarsH Oct 21 '13 at 20:49
up vote 25 down vote accepted

You might have a look at zgrep.

>$ zgrep -h
grep through gzip files
usage: zgrep [grep_options] pattern [files]
share|improve this answer

The zgrep program exists for this specific purpose.

share|improve this answer

Or, if your OS doesn't come with zgrep, something like this.

gunzip -c somefile1.gz [...] | grep 'string'
share|improve this answer
Which is how zgrep is actually implemented, as it is just a shell script (with some additional stuff to handle arguments and report errors properly). – liori Oct 21 '13 at 20:57

There's a utility called zcat, which is a version of cat that works on gzipped files. In your situation, you could do something like the following:

zcat yourfile.gz | grep 'serverfault is awesome'
share|improve this answer
Useless use of cat: – fukawi2 Oct 21 '13 at 22:34

While zgrep appears to be the next best thing since sliced bread, I thought I'd post an alternative...

You can use piping to unzip the log files before grepping them, without taking up disk space. For example:

cat /var/log/program.log.*.gz | gunzip | grep 'stuff and things'
share|improve this answer
Damn... Apparently, I was a bit late to the party... – Soviero Oct 21 '13 at 14:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.