Recursive Zgrep Not Working

Recursive Zgrep Not Working

I have a directory hierarchy that contains numerous .gz files. I want to be able to recursively grep them for the string "foo". From what I've read online the following should work:

zgrep -R -H "foo" .

However, this never returns any results. If I replace the dot with the name of a file it does work. For example,

zgrep -R -H "foo" myFile.gz

however, obviously, this no longer will be recursive.

I know "foo" is in some of the files because the following command returns many results:

find . -iname "*.gz" | xargs zgrep "output" | less

Does anyone know why my recursive zgrep command is not working. I'm on a RHEL linux box

5 Answers

The way I usually do is:

zgrep "foo" $(find . -name "*.gz")

or (however, the file name will be printed before each result instead of each line --not just the files with matches--):

find . -name "*.gz" -print -exec zgrep "foo" {} \;

If that command returns "Argument list too long", try this way:

for I in $(find . -name "*.gz"); do zgrep "foo" $I; done
3

You have to install zutils. This will replace the default and limited zgrep on your system with a recursive-able one.

On debian based systems you run apt-get install zutils then you can zgrep -rH myword . and use most of the other parameters from grep you know and love.

2

Your almost there. Try this:

zgrep -R -H "foo" *.gz

EDIT: Hmmmm.... intriguing!

According to my zgrep, -R (Recursive) is not an option. Its simply not supported. Id have a check to see what the man page of your zgrep says.

One alternative, which depends on only one level of subdirectories is to do this:

zcat */*.gz | grep <needle>

But I would suggest that your find command is probably better!

1

Use sift - it's super-fast.

sift -z foo

-z is for unpacking gzips, it descends into subdirectories by default. Shows filenames also by default (your -H option).

PS. Thanks to your question I discovered this tool today. Just battletested it, apart from the functionality you need, I have a 5x speedup on some production logs when comparing to zgrep.

it also works with:

zgrep "foo" */ *.gz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.