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?
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.)
xxd -p file
Or if you want it all on a single line:
xxd -p file | tr -d '\n'
Perl one-liner:
perl -e 'local $/; print unpack "H*", <>' file
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.
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
.)