There's a lot of confusion here because there isn't just one zgrep
. I have two versions on my system, zgrep
from gzip
and zgrep
from zutils
. The former is just a wrapper script that calls gzip -cdfq
. It doesn't support the -r, --recursive
switch.1
The latter is a c++
program and it supports the -r, --recursive
option.
Running zgrep --version | head -n 1
will reveal which one (if any) of them is the default:
zgrep (gzip) 1.6
is the wrapper script,
zgrep (zutils) 1.3
is the cpp
executable.
If you have the latter you could run:
zgrep 'pattern' -r --format=gz /path/to/dir
Anyway, as suggested, find
+ zgrep
will work equally well with either version of zgrep
:
find /path/to/dir -name '*.gz' -exec zgrep -- 'pattern' {} +
If zgrep
is missing from your system (highly unlikely) you could try with:
find /path/to/dir -name '*.gz' -exec sh -c 'gzip -cd "$0" | grep -- "pattern"' {} \;
but there's a major downside: you won't know where the matches are as there's no file name prepended to the matching lines .
1: because it would be problematic