Grepping Compressed Files

Problem

You need to grep some compressed files. Do you have to uncompress them first?

Solution

Not if you have zgrep, zcat, or gzcat on your system.

zgrep is simply a grep that understands various compressed and uncompressed files (which types are understood varies from system to system). You will commonly run into this when searching syslog messages on Linux, since the log rotation facilities leave the current log file uncompressed (so it can be in use), but gzip archival logs:

$ zgrep 'search term' /var/log/messages*

zcat is simply a cat that understands various compressed and uncompressed files (which types are understood varies from system to system). It might understand more formats than zgrep, and it might be installed on more systems by default. It is also used in recovering damaged compressed files, since it will simply output everything it possibly can, instead of erroring out as gunzip or other tools might.

gzcat is similar to zcat, the differences having to do with commercial versus free Unix variants, and backward compatibility:

$ zcat /var/log/messages.1.gz

Discussion

The less utility may also be configured to transparently display various compressed files, which is very handy. See Doing More with less.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.255.127