How to Use the od Command to Dump Files in Octal Format in Linux

The Danger of Invisible Characters

If you are a systems administrator troubleshooting a broken script, you will frequently encounter errors caused by invisible characters. You might copy a perfectly written block of code from a website and paste it into a Linux file, only to watch the script violently crash. When you open the file in the nano text editor, the code looks absolutely perfect. Why did it crash?

The human-readable text editor is lying to you. It is hiding the fact that the website you copied the code from injected invisible “carriage return” characters (\r) or non-standard Unicode spaces into the file. The Linux interpreter chokes on these invisible anomalies, but because you cannot physically see them on the screen, you cannot fix them.

To completely strip away the illusion of human-readable text and dump the raw, foundational mathematical bytes of a file directly onto your screen, you must use the od (Octal Dump) command.

Step 1: The Basic Octal Dump

The od command is a forensic utility designed to translate the binary data of a file into octal (Base-8) mathematics. By translating the file into pure math, invisible characters are instantly exposed as unique numerical codes.

Assume you have a broken script named script.sh.

od script.sh

The terminal will instantly output a wall of numbers. The numbers on the far left represent the byte offset (exactly how deep into the file you are). The columns to the right contain the actual octal translation of the file’s data.

While this is mathematically accurate, reading pure octal data is incredibly difficult unless you are a seasoned assembly programmer.

Step 2: The Character Translation Flag

To make the output useful for troubleshooting invisible characters, you must force the od command to display the data as raw ASCII characters instead of raw octal math.

You do this using the -c (character) flag.

od -c script.sh

This completely transforms the output. The command takes the raw file and spaces every single character out into a perfect grid. Normal letters look normal (e.g., a, b, c). However, invisible characters are ruthlessly exposed.

A standard space becomes a blank spot in the grid. A standard Linux newline becomes \n. A hidden Windows carriage return (which is notoriously responsible for breaking Linux scripts) is blatantly exposed as a massive \r.

If you see a \r hiding at the end of your lines of code, you have instantly found the invisible anomaly crashing your script.

Step 3: Dumping in Hexadecimal

While the Octal Dump command is named after octal math, modern cybersecurity forensics rely heavily on Hexadecimal (Base-16) math. If you are analyzing a corrupted binary file (like a broken JPEG image) to see if its file header is intact, you need a hexadecimal dump.

You can force the od command to output in hexadecimal using the -x flag.

od -x corrupted_image.jpg | more

The terminal will output the classic hacker matrix of two-byte hexadecimal blocks (e.g., ffd8, ffe0). You can analyze the very first block of data (the Magic Number) to mathematically prove the identity of the file, regardless of what the file extension claims it is.

Step 4: Isolating Specific Data Blocks

If you only want to analyze a very specific chunk of a massive file, dumping the entire file is a waste of time and terminal space. You can use the -j (skip) and -N (read bytes) flags to surgically extract data.

For example, if you want to skip the first 100 bytes of a file, and then only dump the next 16 bytes in character mode, you would run:

od -j 100 -N 16 -c script.sh

The command instantly jumps exactly 100 bytes deep into the file, analyzes the next 16 bytes, and completely ignores the rest. By mastering the od command, you gain the ability to forensically audit the raw data of any file on the system, guaranteeing that invisible errors can never hide from you.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.