8

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?

5 Answers5

25

You might have a look at zgrep.

>$ zgrep -h
grep through gzip files
usage: zgrep [grep_options] pattern [files]
mveroone
  • 484
10

The zgrep program exists for this specific purpose.

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

Emyl
  • 380
  • 2
  • 11
4

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

gunzip -c somefile1.gz [...] | grep 'string'
USD Matt
  • 5,411
  • 18
  • 26
3

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'
Boscoe
  • 574
1

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'
Soviero
  • 4,426