The Limitation of the cat Command
If you need to view the contents of a file in the Linux terminal, the standard approach is to use the cat or less command. These commands are designed to read standard ASCII text files. If you run cat config.txt, you will see a perfectly readable string of characters.
However, if you attempt to use cat on a compiled binary executable, an image file, or a corrupted text file containing hidden, non-printable control characters, the terminal will output a garbled mess of bizarre symbols, and it might even crash your terminal session entirely.
To safely inspect the raw, foundational data of any file—regardless of whether it contains text or compiled machine code—you must use the od (Octal Dump) command.
Step 1: The Default Octal Output
The od command reads the raw bytes of a file and converts them into a human-readable numeric format. By default, it outputs the data in octal (base-8) format.
To view the raw bytes of a file named secret.dat, simply run:
od secret.dat
The output will look something like this:
0000000 062110 066154 020157 067127 071162 005144
0000014
The first column (0000000, 0000014) represents the byte offset—the exact position in the file where that line of data begins. The remaining columns are the actual contents of the file, translated into octal numbers. Two bytes are grouped together to form one octal “word.”
Step 2: Viewing Files in Hexadecimal
While octal is the default due to its legacy Unix origins, modern developers and reverse engineers almost exclusively read raw data in hexadecimal (base-16) format.
To force od to display the file contents in hexadecimal, use the -x flag:
od -x secret.dat
The output is now formatted in hex (e.g., 6848 6c65 6f6c 5720 726f 646c 0a21), which perfectly matches the output style of standard hex editor applications.
Step 3: Viewing Characters Alongside the Data
If you are analyzing a corrupted text file, or trying to find a hidden string password buried inside a compiled binary, staring at a wall of pure hexadecimal numbers is incredibly difficult.
You can use the -c (character) flag to force od to display the data as standard ASCII characters. If a byte represents a non-printable control character (like a line break or a tab), od will safely display it as an escaped character (e.g., \n or \t).
od -c secret.dat
0000000 H e l l o W o r l d ! \n
0000014
This is the most powerful use case for system administrators. If a bash script is mysteriously failing to execute, running od -c script.sh might reveal that the file is infested with invisible Windows-style carriage return characters (\r), allowing you to immediately diagnose the syntax error.
Step 4: Combining Formats for Deep Analysis
The od command allows you to stack formats on top of each other, creating a highly detailed diagnostic view. You can use the -t (type) flag to manually specify exactly how you want the bytes decoded.
If you want to view the raw hexadecimal numbers (x1 means one-byte hex) directly stacked on top of the ASCII characters (c), you can run:
od -t x1c secret.dat
0000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 21 0a
H e l l o W o r l d ! \n
This combined view provides an absolute, infallible understanding of exactly what data is written to the hard drive, proving invaluable for low-level debugging and security analysis.