zgrep
Overview
zgrep runs grep over gzip-compressed files without manual decompression. Useful for rotated logs (*.gz). Variants exist for other compressors (xzgrep, bzgrep, zstdgrep).
Syntax
zgrep [grep-options] pattern [file...]Examples with Explanations
Search compressed logs
zgrep -H 'error' /var/log/nginx/access.log.*.gz
zgrep -E 'HTTP/1\.[01]" 5[0-9]{2}' access.log.2.gzRecursive pattern over mixed trees
find /var/log -name '*.gz' -print0 | xargs -0 zgrep -H 'OutOfMemory'Count matches
zgrep -c 'ERROR' app.log.gzNotes & Pitfalls
- Performance is worse than grepping already-decompressed data — decompress hot logs if you query them constantly.
- Binary matches: use
grepflags carefully (-a,-I).
- Confirm whether your
zgrepshells out togzip -dc+grep.
Additional Resources
man zgrep