14

hd and od are both dump viewers of binary content. Can hd be used wherever od is and vice versa?

Caleb
  • 69,278
  • 18
  • 196
  • 226
Tim
  • 98,580
  • 191
  • 570
  • 977

2 Answers2

24

hd is a synonym for hexdump -C on FreeBSD and on some Linux distributions. hexdump is from the BSD days; od is from the dawn of time. Only od is standardized by POSIX. The Single UNIX rationale discusses why od was chosen in preference to hd or xd.

These commands do very similar things: display a textual representation of a binary file, using octal, decimal or hexadecimal notation. There's no fundamental difference between the two.

They have many options to control the output format, and some formats can only be achieved with one or the other command. In particular, to see a glance of what's in a binary file, I like hd's output format, with a column on the right showing printable characters literally; od can't do that.

$ od /bin/sh | head -n 2                # od default: octal, 2-byte words
0000000 042577 043114 000402 000001 000000 000000 000000 000000
0000020 000002 000076 000001 000000 170020 000101 000000 000000

$ od -Ax -t x1 /bin/sh | head -n 2      # od showing bytes in hexadecimal
000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
000010 02 00 3e 00 01 00 00 00 10 f0 41 00 00 00 00 00

$ hd /bin/sh | head -n 2                # hd default output: nice
00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  02 00 3e 00 01 00 00 00  10 f0 41 00 00 00 00 00  |..>.......A.....|
Caleb
  • 69,278
  • 18
  • 196
  • 226
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • Thanks! In octal output by od, does a digit mean three bits? So for example `042577` is 18 bits i.e. 9/4 bytes? – Tim Jul 24 '11 at 14:42
  • @Tim Yes, [octal](http://en.wikipedia.org/wiki/Octal) means each digit corresponds to 3 [bits](http://en.wikipedia.org/wiki/Bit) (and [hexadecimal](http://en.wikipedia.org/wiki/Hexadecimal), 4 bits). 042577 actually represents 16 bits (2 bytes); the first digit in a block in this format will always be 0 or 1. octal(042577) = hexadecimal(7f45) = decimal(32581). – Gilles 'SO- stop being evil' Jul 24 '11 at 15:04
  • Thanks! I read from other sources that octal numbers start with an additional 0. What does it mean when it starts with 1? – Tim Jul 24 '11 at 15:28
  • @Tim In many programming languages, `123` is a number represented in base 10 (decimal), whereas `0123` means that the number is represented in base 8 (octal), i.e. `0123` is 83. `0x123` would be in base 16 (hexadecimal), i.e. 291. This is unrelated to the `od` output format. – Gilles 'SO- stop being evil' Jul 24 '11 at 15:46
  • Many? I'd say any C-like and derivatives — yep, indeed. What else? How many? – poige Jan 25 '19 at 13:45
  • @poige C, C++, Java, Perl, PHP, Ruby, Python ≤2, Objective-C, Javascript, Go, … – Gilles 'SO- stop being evil' Jan 25 '19 at 14:31
  • C++, Java, Ruby, PHP, Objective C, Javascript, Go — all they are heavily influenced by C syntax. Only Python isn't. – poige Jan 25 '19 at 14:46
  • @poige Gilles' list includes most languages in current active use. (Obvious exceptions are C# and Swift.) Most languages have been heavily influenced by C syntax, but describing Java as "C-like" stretch the description to the point of meaninglessness. (You missed "Perl" from the list of "not heavily influenced" btw.) – Martin Bonner supports Monica Jun 11 '19 at 07:54
8

Actually od can display both hex/oct/dec and printable chars:

$ od -Ax -tx1z /bin/sh | head -n2
000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00  >.ELF............<
000010 03 00 3e 00 01 00 00 00 32 4e 00 00 00 00 00 00  >..>.....2N......<

Main difference, I guess, is only historical. Also some versions hd can color output.

slm
  • 363,520
  • 117
  • 767
  • 871
hurufu
  • 317
  • 3
  • 4