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.

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

share|improve this question
    
...and don't forget xxd too! :D –  Aaron D. Marasco Jul 25 '11 at 0:24
add comment

1 Answer

up vote 6 down vote accepted

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.....|
share|improve this answer
    
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 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). –  Gilles 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 Jul 24 '11 at 15:46
add comment

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.