aj3423
aj3423

Reputation: 2561

How to search binary data in target directory?

I tried ag and some other searcher, they only support searching normal text in binary files, but not support searching binary text in binary files.

I googled for "search binary text" and results all talking about "search text in binary file".

Any tool support searching like this search_tool -bin "313233"? (which is actually searching for files contain string "123")

Upvotes: 1

Views: 1055

Answers (3)

aj3423
aj3423

Reputation: 2561

I just wrote one using Golang, simple but works.

https://github.com/aj3423/bingrep

Upvotes: 0

voidbar
voidbar

Reputation: 141

Without knowing your requirements, this should suffice for most scenarios:

grep -robUaP "{YOUR_BINARY_SEQUENCE_HERE_IN_HEX_FORMAT}" {DIRECTORY_TO_SEARCH}

Example:

grep -robUaP "\x01\x02\x03" /bin

Based on: Binary grep on Linux?

Upvotes: 1

Rafael
Rafael

Reputation: 7746

Let's build our own POSIX-compliant tool:

search_tool:

od -An -tx1 "$1" | tr -cd '[:xdigit:]' | grep -qF "$2"

This dumps the file in hexadecimal, removes non-hex digits, and searches the result.

$ chmod +x search_tool
$ ./search_tool somefile 313233
$ echo $?
0

somefile contains 123.


You can use find(1) to run search_tool in any directory:

$ find java_playground ! -type d -a -exec search_tool {} 313233 \; -print

search_tool must be in locatable from PATH.

Upvotes: 1

Related Questions