Bgrep: a binary grep

About

I’m terribly annoyed by the fact that grep(1) cannot look for
binary strings. I’m even more annoyed by the fact that a simple
search for “binary grep” doesn’t yield a tool which could do that.
So I wrote one: bgrep.c.

Felix “tmbinc” Domke in April, 2009

Setup

curl -L 'https://github.com/tmbinc/bgrep/raw/master/bgrep.c' | gcc -O2 -x c -o /usr/local/bin/bgrep -

Usage

Usage is simple: Just give it a hex string (without spaces) as first argument, and possibly use ?? to mask out bytes. It will print the file names corresponding offsets where the binary pattern matches.

Licensing

Feel free to modify, branch, fork, improve. Re-licenses as BSD.
UPDATE: I’ve relocated bgrep.c to github. Feel free to send pull-requests!

Resources


Thanks for sharing the other day, @weef!

Example

Task

Find all PNG files in curent directory, without using the file utility for decoding the magic number, but rather by bgrepping through them and then filtering out which files actually carry the PNG magic number at their beginning.

This

seems to work pretty good.

$ bgrep 89504e . | grep 00000000
./Screenshot_20190112-211138.png: 00000000
./Screenshot_20190112-210806.png: 00000000
./ip-tools-icon-256.png: 00000000
./portier-avatar.png: 00000000
./Thunderbird/Screenshot mozilla.png: 00000000
./grey_bellhop_icon-icons.com_59445.png: 00000000
./01/Screenshot_20190113-080905.png: 00000000
./01/Screenshot_20190113-080929.png: 00000000
./01/Screenshot_20190113-080909.png: 00000000
./01/Screenshot_20190113-080940.png: 00000000
./ip-tools-icon-512.png: 00000000
./ip-tools-icon-128.png: 00000000