Recursive zgrep not working

42,826

Solution 1

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

Solution 2

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.

Solution 3

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!

Share:
42,826

Related videos on Youtube

Nosrettap
Author by

Nosrettap

I recently graduated from Duke University as a computer science and economics double major. I am now working full time as a software developer. I am proficient in Objective-C and Java, and I know (to some degree) C, C++, Python, Perl, SML, Assembly, HTML, CSS, JavaScript.

Updated on September 18, 2022

Comments

  • Nosrettap
    Nosrettap over 1 year

    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

  • Nosrettap
    Nosrettap over 10 years
    When I try that I get: zsh: no matches found: *.gz
  • nzn
    nzn over 8 years
    Hi down-voter. Would love to know why this is not a good idea. I personally used it and I thought it was great.
  • lepe
    lepe over 8 years
    (+1) Not sure why the down vote. For those interested, there is a nice explanation about the zutils version vs the gzip version of zgrep here: unix.stackexchange.com/questions/187742/…
  • kos
    kos about 4 years
    find . -name "*.gz" for cross-platform compatibility on all POSIX systems
  • lepe
    lepe about 4 years
    @kos: Added the dot for compatibility. Thank you.
  • tom
    tom over 2 years
    zgrep ... $(find ...) breaks on filenames with spaces or other special characters. I recommend find . -name "*.gz" -exec zgrep "foo" {} +. This prints the filename on each matching line, unlike find ... -exec ... {} \; (another way to print the filename on each matching line is to use zgrep -H, if grep supports it). See also: unix.stackexchange.com/questions/187742/…