For hysterical historical reasons, od
prints two-byte words¹ by default.
The number 020061 (octal) corresponds to the two-byte sequence 1␣
(␣
is a space character). Why? It's clearer if you use hexadecimal: 0o20061 = 0x2031, and ␣
is 0x20 (32) in ASCII and 1
is 0x31 (49). Notice that the lower-order bits (0x31) correspond to the first character and the higher-order bits correspond to the second character: od is assembling the words in little-endian order, because that happens to be your system's endianness.²
Little-endian order is not very natural here because one of the output formats (-c
) prints characters, the other one (-o
) prints words. Each word is printed as a number in the usual big-endian notation (the most significant digit comes first in our left-to-right reading order). This is even more apparent in hexadecimal where the byte boundaries are clearly apparent in the numerical output:
echo '1 text' | od -xc
0000000 2031 6574 7478 000a
1 t e x t \n\0
If you prefer to view the file as a sequence of bytes, use od -t x1
(or hd
if you have it).
¹
Once upon a time, men were real men, computers were real computers, numbers were often written in octal, and words were two bytes long.
²
All PCs (x86, x86-64) are little-endian, as was the PDP-11 where Unix started. ARM CPUs can cope with either endianness but Linux and iOS use it in little-endian mode. So most of the platforms you're likely to encounter nowadays are little-endian.
When you pipe the output, ls
acts differently.
This fact is hidden away in the info documentation:
If standard output is a terminal, the output is in columns (sorted vertically) and control characters are output as question marks; otherwise, the output is listed one per line and control characters are output as-is.
To prove it, try running
ls
and then
ls | less
This means that if you want the output to be guaranteed to be one file per line, regardless of whether it is being piped or redirected, you have to run
ls -1
(-1
is the number one)
Or, you can force ls | less
to output in columns by running
ls -C
(-C
is a capital C)
Best Answer
Assuming you mean the byte offset, from
man od
so for example
od -An file