Linux – Recursive zgrep not working

bashcommand linegreplinux

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

Best Answer

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
Related Question