14

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

1

2 Answers 2

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.....|
8
  • 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, 2011 at 14:42
  • @Tim Yes, octal means each digit corresponds to 3 bits (and 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). Jul 24, 2011 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, 2011 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. Jul 24, 2011 at 15:46
  • Many? I'd say any C-like and derivatives — yep, indeed. What else? How many?
    – poige
    Jan 25, 2019 at 13:45
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.

3
  • OSX od does not support the z suffix in -tx1z
    – tripleee
    Oct 18, 2016 at 7:32
  • Actually the biggest trouble would be to remember that train of args instead of one hd. Nevertheless, useful, thank you :)
    – poige
    Jan 25, 2019 at 13:47
  • @poige, alias hd='od -Ax -tx1z' may help if you don't want to remember all those flags.
    – hurufu
    Jan 6, 2022 at 15:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .