Decoding Binary Files with Octal Dump

May 01, 2017

How to set Linux file permissions through Octal notation

In many cases with Unix/Linux, if you want to view the contents of a file, using the cat command works just fine. The phrase "cat samplescript.txt", will reveal, at the command line, the content of that file.

However, cat won't work for binary files, because binary files contain non-printing characters (Or non-ASCII characters). Run a cat on a binary program, such as sed, will only get you a screen full of gibberish, and may even destroy the terminal session itself.

Storing programs as binary files is more efficient than storing them in ASCII, largely because binary programs use all eight bits in a byte [up to 256 possible combinations], whereas ASCII only uses seven [128 combinations] leaving the last bit to sign the byte.

What Octal Dump (od from the command line) does is display the contents of a binary file, including an execution files, as sets of octals.

As the name suggests, the octal numbering system is a numbering system in base eight. When used with the "-bc" option, he od program renders each byte of the program in octal.

For instance, rendering this command from the command line in the /bin directory of binary files:

od -bc sed

will return a row of six digit octals, preceded by a seven digit number that is the offset, or position, of the first byte in the line. Below each octal is a its conversion into ASCI characters, if the resulting decimal conversion falls between decimal 33 and 127.

As an aside, to convert from octal to decimal yourself, simply multiply each digit of the octal number by a successive power of eight, going from right to left. So, if the octal is 114, then you would calculate (1* [8^2] + 1 * [8^1] + 4 * [8 ^ 0]), which would equal (64 + 8 + 4), which would equal 76

--Notes from Sumitabha Das' book "Your UNIX: The Ultimate Guide" as well as from a UMUC class on Unix I was at the time (2010).

See also " A (Somewhat) Easy Trick to Understanding File Permission Octals."