9

I have problem executing zgrep command for looking for word in the directory where I have almost 1000 *.gz files.

I am trying with:

find /var/www/page/logs/ -name "*.gz" -exec zgrep '/index.php' {} \;

result is:

GET : '/index.php'
GET : '/index.php'
GET : '/index.php'

And it works.I get list of occurance of index.php with no file name where it was found. It is kind of useless for me unless i knew in which files (filename) it appears.

How can i fix it?

Mithrand1r
  • 2,313
  • 9
  • 37
  • 76

2 Answers2

15

You could supply the -H option to list the filename:

find /var/www/page/logs/ -name "*.gz" -exec zgrep -H '/index.php' {} \;

If you wanted only the list of matching files, use -l:

find /var/www/page/logs/ -name "*.gz" -exec zgrep -l '/index.php' {} \;
devnull
  • 118,548
  • 33
  • 236
  • 227
4

Make grep search in two files, then it will tell you which one it found it in:

find /var/www/page/logs/ -name "*.gz" -exec zgrep '/index.php' {} /dev/null \;

And it won't take long to search in /dev/null.

Here is an example without /dev/null:

zgrep chr your.gz
>chrMCHU - Calmodulin - Human, rabbit, bovine, rat, and chicken
>chrgi|5524211|gb|AAD44166.1| cytochrome b [Elephas maximus maximus]

and with /dev/null

zgrep chr your.gz /dev/null
your.gz:>chrMCHU - Calmodulin - Human, rabbit, bovine, rat, and chicken
your.gz:>chrgi|5524211|gb|AAD44166.1| cytochrome b [Elephas maximus maximus]
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Although giving the expected result, I do not think it should be encouraged, especially with such an obvious correct way of doing it (which is just a `man grep` away, too). It might have its place, but to suggest it for a beginner seems counterproductive in the long run. – plundra Feb 04 '14 at 10:37
  • As a Perl enthusiast (motto "There's More Than One Way To Do It") I would dispute that there is "an obvious correct way". Not all greps have the "-H" option, and the /dev/null trick is actually advocated by Grymoire here http://www.grymoire.com/unix/Grep.html – Mark Setchell Feb 04 '14 at 10:46
  • I've inverted my vote. It was not as common or old as I believed it to be actually, my bad! Although current BSDs, Busybox and of course GNU includes it, Solaris did not and it was only fairly recent (>2000) in some. – plundra Feb 04 '14 at 11:26
  • Thank you. Out of interest, how/where can you research variants like this? Is there a good reference website describing version differences for Unix tools? – Mark Setchell Feb 04 '14 at 11:30
  • Most projects have the man-pages available easy online, for the system I can't do a quick ssh to. In this case, checked OS X, FreeBSD and Solaris man-pages online (top results on google), as well as checked the source history in cvsweb for Open and NetBSD. There are cross referencing sites though (man-pages from different OS as well as soruce), but have yet to familiarize my self with a particular one. – plundra Feb 04 '14 at 12:44