Od

From LQWiki
Jump to navigation Jump to search

Command od

Dump an unambiguous representation of a file or user input to stdout in octal, decimal, hex or ASCII format. Very useful for determining just what control characters are in a file or filename.

Syntax

od [OPTION]... [FILE]...
od --traditional [FILE] [[+]OFFSET [[+]LABEL]]

NOTE: This page was based on the 4.5.3 coreutils man page included with Red Hat 9. For a current definition please see the od reference at the GNU site

Options

-A, --address-radix=RADIX

   decide how file offsets are printed

-j, --skip-bytes=BYTES

   skip BYTES input bytes first

-N, --read-bytes=BYTES

   limit dump to BYTES input bytes

-s, --strings[=BYTES]

   output strings of at least BYTES graphic chars

-t, --format=TYPE

   select output format or formats

-v, --output-duplicates

   do not use * to mark line suppression

-w, --width[=BYTES]

   output BYTES bytes per output line

--traditional

   accept arguments in traditional form

--help display this help and exit

--version

 output version information and exit

Traditional format specifications may be intermixed; they accumulate: -a same as -t a, select named characters

-b same as -t oC, select octal bytes

-c same as -t c, select ASCII characters or backslash escapes

-d same as -t u2, select unsigned decimal shorts

-f same as -t fF, select floats

-h same as -t x2, select hexadecimal shorts

-i same as -t d2, select decimal shorts

-l same as -t d4, select decimal longs

-o same as -t o2, select octal shorts

-x same as -t x2, select hexadecimal shorts

For older syntax (second call format), OFFSET means -j OFFSET. LABEL is the pseudo-address at first byte printed, incremented when dump is progressing. For OFFSET and LABEL, a 0x or 0X prefix indicates hex- adecimal, suffixes may be . for octal and b for multiply by 512.

TYPE is made up of one or more of these specifications:

a named character

c ASCII character or backslash escape

d[SIZE]

      signed decimal, SIZE bytes per integer

f[SIZE]

      floating point, SIZE bytes per integer

o[SIZE]

      octal, SIZE bytes per integer

u[SIZE]

      unsigned decimal, SIZE bytes per integer

x[SIZE]

      hexadecimal, SIZE bytes per integer

SIZE is a number. For TYPE in doux, SIZE may also be C for sizeof(char), S for sizeof(short), I for sizeof(int) or L for sizeof(long). If TYPE is f, SIZE may also be F for sizeof(float), D for sizeof(double) or L for sizeof(long double).

RADIX is d for decimal, o for octal, x for hexadecimal or n for none. BYTES is hexadecimal with 0x or 0X prefix, it is multiplied by 512 with b suffix, by 1024 with k and by 1048576 with m. Adding a z suffix to any type adds a display of printable characters to the end of each line of output. --string without a number implies 3. --width without a number implies 32. By default, od uses -A o -t d2 -w 16.

Examples

A simple one

$ echo "hello world" | od
0000000 062550 066154 020157 067567 066162 005144
0000014

As mentioned above, od can be used to locate strange characters in files. Imagine you are writing a program that takes input from a file and for some reason it just doesn't work with a particular file, run it through od and you may see why:

# cat afile
<his is a file with a non-printing control character here >

So it looks like the file afile contains the above text. Let's make sure:

# od -cb afile
0000000   T   h   i   s       i   s       a       f   i   l   e       w
        124 150 151 163 040 151 163 040 141 040 146 151 154 145 040 167
0000020   i   t   h       a       n   o   n   -   p   r   i   n   t   i
        151 164 150 040 141 040 156 157 156 055 160 162 151 156 164 151
0000040   n   g       c   o   n   t   r   o   l       c   h   a   r   a
        156 147 040 143 157 156 164 162 157 154 040 143 150 141 162 141
0000060   c   t   e   r       h   e   r   e       >  \r   <  \n
        143 164 145 162 040 150 145 162 145 040 076 015 074 012

As you can see, just using cat doesn't show you the character. When we run the file through od we can see that there is actually a carriage return in there BETWEEN the '>' and '<' characters but cat sees it as a command to move back to the start of the line so we don't see the T at the start of the line as it gets overwritten by the final < character!


Provided by

Most (all?) Linux distributions incorporate this from the GNU Coreutils project and use its man page

Related Commands

These all relate to the output or processing of entire files

  • hexdump -- similar program specific to dumping in hexidecimal.
  • cat -- concatenate files into a single result.
  • tac -- line-reversed cat
  • rev -- character-reversed cat
  • nl -- output with line numbers
  • base32 -- Encode in printable characters.
  • base64 -- Encode in printable characters.