General
cancel
Showing results for 
Search instead for 
Did you mean: 

Grep Recursively and find a pattern in gzipped files on Linux

Grep and replace
Occasional Contributor

Grep Recursively and find a pattern in gzipped files on Linux

I have to grep certain patterns recursively, in sub directories . The log files are gzipped and I am on Linux. How can I do that?

e.g
grep /A/B/C/D/E/log1.gz
grep /A/B/C/F/G/log2.gz

please advise
4 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: Grep Recursively and find a pattern in gzipped files on Linux

Hi:

This should work.

# find /path -xdev -type f -name "*.gz" | xargs -n1 -t gzip -d -c | grep pattern

For example, given '/tmp/hosts.gz' and '/tmp/services.gz' --- copies of the standard files, gzipped:

# find /tmp -xdev -type f -name "*.gz" | xargs -n1 -t gzip -d -c |grep local

...produces:

gzip -d -c /tmp/hosts.gz
127.0.0.1 localhost loopback
gzip -d -c /tmp/services.gz
hacl-local 5304/tcp # HA Cluster Commands

...by using the trace option of 'xargs' and processing one argument at a time ('-n 1') we can show the filename in which the match occurs. Your files are only uncompressed on-the-fly. They are left unchanged on disk.

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: Grep Recursively and find a pattern in gzipped files on Linux

Hi (again):

My original post was done on HP-UX. On a Linux server, substitute '-L1' for '-n1' in the options to 'xargs'.

Thus, for Linux:

# find /tmp -xdev -type f -name "*.gz" | xargs -L1 -t gzip -d -c |grep local

Regards!

...JRF...
Huc_1
Honored Contributor

Re: Grep Recursively and find a pattern in gzipped files on Linux

Hey nice! that... to futher this

On linux you could do
# find /tmp -xdev -type f -name "*.gz" | xargs -n1 zgrep local

that is use zgrep ... this is a grep on a gzip file.

man zgrep
for more on this command.

Jean-pierre Huc
Smile I will feel the difference
Stuart Browne
Honored Contributor

Re: Grep Recursively and find a pattern in gzipped files on Linux

If you're going to use 'xargs -n1', then you may as well use:

find /tmp -xdev -type f -name "*.gz" -exec zgrep -H local {} \;

The '-H' will print the filename of the match, along with the matched line. if you just want the filename, use '-l' in the grep.
One long-haired git at your service...