views:

518

answers:

5

How do I create a UNMODIFIED hex dump of a binary file in linux with bash? The od and hexdump commands both insert spaces in the dump, I DON'T WANT THIS, I need something that will simply write a long string with all the hex characters without inserting spaces or newlines in the output. How do I do this in bash?

+1  A: 

It seems to depend on the details of the version of od. On OSX, use this:

od -t x1 -An file |tr -d '\n '

(That's print as type hex bytes, with no address. And whitespace deleted afterwards, of course.)

Donal Fellows
+5  A: 

xxd -p file

Or if you want it all on a single line:

xxd -p file | tr -d '\n'

mark4o
+1  A: 

Perl one-liner:

perl -e 'local $/; print unpack "H*", <>' file
Alan Haggai Alavi
+2  A: 

Format strings can make hexdump behave exactly as you want it to (no whitespace at all, byte by byte):

hexdump -e '1/1 "%.2x"'

EDIT (fix asterisks):

hexdump -ve '1/1 "%.2x"'

1/1 means "each format is applied once and takes one byte", and "%.2x" is the actual format string, like in printf. In this case: 2-character hexadecimal number, leading zeros if shorter.

Michał Trybus
You need a `-v` or it will drop repeated bytes and replace them with an asterisk.
Dennis Williamson
Yes, that's right, I missed it.
Michał Trybus
A: 

The other answers are preferable, but for a pure Bash solution, I've modified the script in my answer here to be able to output a continuous stream of hex characters representing the contents of a file. (Its normal mode is to emulate hexdump -C.)

Dennis Williamson