1

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")

aj3423
  • 2,003
  • 3
  • 32
  • 70

3 Answers3

1

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.

Rafael
  • 7,605
  • 13
  • 31
  • 46
  • Few things: 1) Seeking recommendation for tools is grounds to close the question. 2) You said "any tool support" 3) You did not state non-functional requirements in your question. 4) That's entirely dependent upon the implementation. 5) This answer satisfies the functional requirements stated in the question. – Rafael Aug 13 '21 at 01:13
  • Thanks, but I mean search in files in a directory, not in single file, like "ag", "grep"... – aj3423 Aug 13 '21 at 09:56
  • Given search_tool, I figured you would have your own mechanism to execute it for files in a directory. There's no guarantee other tools would provide that functionality either. Anyway, I provided a POSIX-compliant way to do so. – Rafael Aug 13 '21 at 14:31
1

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?

voidbar
  • 141
  • 2
  • 7
0

I just wrote one using Golang, simple but works.

https://github.com/aj3423/bingrep

aj3423
  • 2,003
  • 3
  • 32
  • 70