Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

5 Answers 5

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.

http://linux.about.com/library/cmd/blcmdl1_zgrep.htm

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
1  
Useless use of cat: partmaps.org/era/unix/award.html#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.