I thought -H always should print filename before matched line. this is true for grep, but not for bzgrep:

$ grep -H pattern file1 
file1: data pattern data

$ bzgrep -H pattern file2.bz2 
(standard input): data pattern data

Is there a workaround for this? I need to grep hundreds of bz2 files, I cannot decompress them all before the search...

  • Try specifying multiple files on the command line – golimar Mar 20 '14 at 11:49
  • command line would be too short for that. There are hundreds of files to be searched. I will use bzgrep in a 'for' loop, where variable will be filename taken from a list, as the files are in different directories. – Washuu Mar 20 '14 at 11:53
  • Then create an empty file and grep both the bz2 file and the empty file each time – golimar Mar 20 '14 at 12:01
  • It is something like workaround, thank you. The only drawback is that the filename will be in separate line, what needs to be parsed correctly. I wonder, however, why such bug went unnoticed. Perhaps I have too old version of bzgrep? I will try to test it on newest CentOS. – Washuu Mar 20 '14 at 12:12

There is (to my knowledge) no version of grep that internally supports gzip / bzip2. Instead, there exist shell scripts such as bzgrep and zgrep, that essentially call the gunzip / bunzip2 binaries and feed the output into grep via stdin. Therefore, the -H flag cannot work as expected when you don't invoke grep directly.

I looked at bzgrep. It was indeed bash script file. The modification of the script in order to fix the issue was quite simple, so I wondered if someone else corrected it in newer version of bzgrep. I found out, it is corrected on Ubuntu and Debian, but not on CentOS.

If someone stumbles on the same issue - just log in to some newer system, and copy the bzgrep shell script. It's the easiest fix.

  • Could you post the required modification in your answer? – Dennis Mar 24 '14 at 21:35

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.