Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In shell, how can I read the bytes of a binary file I have, and print the output as hexadecimal numbers?

share|improve this question

5 Answers 5

up vote 20 down vote accepted

Use hexdump(1)

$ hexdump -x /usr/bin/hexdump 
0000000    feca    beba    0000    0300    0001    0700    0080    0300
0000010    0000    0010    0000    5080    0000    0c00    0000    0700
0000020    0000    0300    0000    00a0    0000    b06f    0000    0c00
0000030    0000    1200    0000    0a00    0100    0010    0000    107c
0000040    0000    0c00    0000    0000    0000    0000    0000    0000
0000050    0000    0000    0000    0000    0000    0000    0000    0000

...

share|improve this answer
3  
Also check out od. There is also a vi-style hex editor called hexer. –  ultrasawblade Apr 7 '11 at 14:47
3  
I like the output of "hexdump -C file" better. xxd is also a nice tool. –  Kambus Apr 7 '11 at 17:09
    
@ultrasawblade +1 it was my next question =) –  Tom Brito Apr 11 '11 at 13:50
    
show in bash hexa format hexdump -e '"\\\x" /1 "%02x"' filename –  Aquarius Power Nov 20 at 16:10

Another option is od:

od -t x1 FILE

or

od -x FILE

od has many options for finetuning.

share|improve this answer
5  
Most people would find od -t x1 as preferred format I guess. –  alex Apr 7 '11 at 16:43
    
Thanks, I'll update my post. –  user unknown Apr 7 '11 at 17:07
    
For the line od -t test I got od: invalid character 't' in type string 'test' –  Tom Brito Apr 11 '11 at 13:52
    
It worked with --t –  Tom Brito Apr 11 '11 at 13:53
    
@Tom: Maybe a different Unix/Linux flavor? My od is: od --version od (GNU coreutils) 7.4 –  user unknown Apr 11 '11 at 15:01

While we're on od and hexdump, two more similar tools:

  • hd (from bsdmainutils)
  • xxd (part of Vim)

Sample output:

$ hd /usr/bin/od | head
00000000  7f 45 4c 46 01 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  02 00 03 00 01 00 00 00  20 8e 04 08 34 00 00 00  |........ ...4...|
00000020  a4 a2 00 00 00 00 00 00  34 00 20 00 08 00 28 00  |........4. ...(.|
00000030  1b 00 1a 00 06 00 00 00  34 00 00 00 34 80 04 08  |........4...4...|
00000040  34 80 04 08 00 01 00 00  00 01 00 00 05 00 00 00  |4...............|
00000050  04 00 00 00 03 00 00 00  34 01 00 00 34 81 04 08  |........4...4...|
00000060  34 81 04 08 13 00 00 00  13 00 00 00 04 00 00 00  |4...............|
00000070  01 00 00 00 01 00 00 00  00 00 00 00 00 80 04 08  |................|
00000080  00 80 04 08 c4 9d 00 00  c4 9d 00 00 05 00 00 00  |................|
00000090  00 10 00 00 01 00 00 00  00 a0 00 00 00 20 05 08  |............. ..|

$ xxd /usr/bin/od | head
0000000: 7f45 4c46 0101 0100 0000 0000 0000 0000  .ELF............
0000010: 0200 0300 0100 0000 208e 0408 3400 0000  ........ ...4...
0000020: a4a2 0000 0000 0000 3400 2000 0800 2800  ........4. ...(.
0000030: 1b00 1a00 0600 0000 3400 0000 3480 0408  ........4...4...
0000040: 3480 0408 0001 0000 0001 0000 0500 0000  4...............
0000050: 0400 0000 0300 0000 3401 0000 3481 0408  ........4...4...
0000060: 3481 0408 1300 0000 1300 0000 0400 0000  4...............
0000070: 0100 0000 0100 0000 0000 0000 0080 0408  ................
0000080: 0080 0408 c49d 0000 c49d 0000 0500 0000  ................
0000090: 0010 0000 0100 0000 00a0 0000 0020 0508  ............. ..

Or, if you want to read the bytes one at a time and print them in your own format, try something like:

while read -n 1 byte; do
    ord=$(printf "%b" "${byte:-\000}" |
          od -t x1 |
          { read offset hex; echo $hex; })
    echo "$ord"
done </usr/bin/od

Sample output:

7f
45
4c
46
01
01
01
00
00
00
share|improve this answer
2  
unlike the other, xxd is also able to revert the modification. With that, it is possible to alter a binary file with shell. –  Offirmo Jun 11 '12 at 16:31
1  
Your while loop doesn't work for backslash and newline characters (and in bash (as opposed to ksh93) for blank characters), nor will it work properly in utf8 locales for bytes with the 8th bit set. Also, you don't need "od" there, you can use printf '%02x\n' "'$byte" –  Stéphane Chazelas Oct 15 '12 at 20:13
    
Note: offset is merely a sort of "dummy variable" here; it has no practical use. It is just used as a placeholder to get to hex. This is what sometimes affects readability in a negative way with read: variables coming out of the blue. –  syntaxerror Dec 2 at 0:01
    
Perhaps. But in this case, the scope of $offset is limited by the subshell, so I don't see it as a problem. –  Mikel Dec 2 at 3:44
mc 

The midnight commander is another option. I don't know whether it is available for all unix flavors. You might need to download it first.
F3 F4 to view in internal editor/ in hex mode.

share|improve this answer

My two cents:

tail -f streamfile | hexdump -C

I like this because you are tailing a currently buffering file while being able to see the hexdump live. Don't forget that EVERYTHING in Unix is a file and we can easily chain commands like tail and hexdump to solve a wide variety of problems.

share|improve this answer
    
tested with for((i=0;i<100;i++));do echo $i >>tst2.bin;sleep 1;done& it works well for monitoring thx :) –  Aquarius Power Nov 20 at 15:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.