Operating System - HP-UX
1834640 Members
3277 Online
110069 Solutions
New Discussion

How to do a find on the gzipped files

 
Hunki
Super Advisor

How to do a find on the gzipped files

There are number of gzipped log files which I need to search for a text , how shall I search these files without gunzipping them.

thanks,
hunki
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor

Re: How to do a find on the gzipped files

Hi Hunki:

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...
Rasheed Tamton
Honored Contributor

Re: How to do a find on the gzipped files

You can also use gzcat

find . -name '*gz' -exec /usr/contrib/bin/gzcat {} \;|grep thestring

gzcat filename.gz|grep thestring

If /usr/contrib/bin is in your path you can just use gzcat. Also you can use it with the xargs option mentioned above by James.

Regards,
Rasheed Tamton.
Rasheed Tamton
Honored Contributor

Re: How to do a find on the gzipped files

This might be more suitable if you have more zipped files to search

for i in `find /tmp -name "*gz"`
do
echo $i
/usr/contrib/bin/gzcat $i|grep string
done

Change /tmp (dir name) according to your find requirement.

Regards,
Rasheed Tamton.