$_ CLI In Depth
Learning progress 0%
lesson text viewing editors

Inspecting Unknown Files: file, od & hexdump

6 min read Module 3: Viewing Files & Text Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:19

Extensions are a hint, not a fact

A file name ending in .txt usually contains text, but the extension is only a convention. Nothing in the file system enforces it. A file named report.pdf may be an HTML page, and a file named data may be a compressed archive. The kernel does not look at the name when it runs a program. It checks the execute permission, and it reads the first bytes of the file to see whether it is a script or a binary.

The file utility follows a similar strategy. It compares the first bytes of a file with a database of known signatures, which are called magic numbers, and it checks some structural rules for text. The database is maintained as a set of rules, so a new format can be added without changing the program.

BASH3 lines
cd /workspace
printf 'hello\n' | gzip > data.gz
file data.gz

The output says that the file is gzip compressed data, even though nothing in the name suggests it. The file utility found the gzip signature at the start of the file.

Reading magic numbers with od

The od command, short for octal dump, prints a file as a series of numbers. By default it uses octal, which is where the name comes from. The option -t x1 prints each byte in hexadecimal, -t c prints each byte as a character or escape, and -A chooses how addresses are displayed. The option -N limits how many bytes are read, which is the normal way to look at a header.

The gzip format starts with the bytes 0x1F and 0x8B, followed by a compression method byte of 0x08 for deflate. Those three bytes are the signature that the file utility used. Seeing them yourself removes the mystery of how the guess was made.

BASH2 lines
cd /workspace
od -An -tx1 -N3 data.gz

The option -An suppresses the address column, so the output is only the bytes. The three values are 1f, 8b, and 08.

hexdump and od compared

The hexdump tool from util-linux prints a similar view, with an address column, the hex values in pairs, and an ASCII column on the right. The ASCII column makes it easy to find strings inside a binary file. Some systems also have xxd, which is installed with the vim package on many distributions. In this lab, od is always present because it comes with GNU coreutils, so it is the tool to learn first. The output of the tools differs in layout, but the bytes are the same.

Whichever tool you use, the goal is the same: find the signature, look for readable strings, and decide what the file is before you process it. A file that looks like text in the first kilobyte may contain binary data later, so the tools help you check beyond the start.

Telling text from binary

A rough test for binary data is whether it contains NUL bytes or many bytes outside the printable range. The file utility uses rules like that, and it reports "data" when no better type matches. Text files may contain the byte 0x00 only in special encodings. If a file that you expected to be text reports data, the next step is to look at the first few lines with od or cat -A rather than to open it in an editor, because an editor may mangle binary content when it saves.

Warning

Do not run a file just to see what it is. Executing an unknown file can run arbitrary code. Inspect it with file and od first, and run it only when you know its source and you understand the risk.

Strings and other helpers

The strings tool prints runs of printable characters from a binary file. It is useful for finding a version string or a path inside a program without reading the whole binary. Lab images may not include it, so the lesson uses od for the examples. The principle is the same: extract the readable parts and judge from them.

Checksums: the other half of identification

Identifying a file by its type tells you what it looks like, but it does not tell you whether it is the file you expected. A checksum answers that question. The coreutils programs md5sum and sha256sum print a digest of the contents, and any change to the bytes changes the digest. When a publisher gives you a checksum for a download, compute the same digest locally and compare the two values before you use the file.

A digest is only as trustworthy as the source of the expected value. A checksum published on the same server as the file protects against corruption, not against an attacker who can change both. Use a checksum from a separate, trusted channel when security matters, and prefer SHA-256 over MD5, which is no longer collision resistant.

Combine the tools in order. First run file to confirm the type, then od to read the signature, then the checksum to confirm the identity, and only then decide whether to open or run the file. Each step costs little and each one removes a class of surprise.

Reading the signature in context

A signature tells you the format, but it does not tell you that the file is complete or uncompressed correctly. A gzip file that was cut short still starts with 1f 8b 08, and file may still report it as gzip compressed data. Tools such as gzip -t test the whole stream, and they report an error if a block is missing. Run the test before you rely on an archive that came from an unreliable transfer.

Keep in mind that od prints bytes in the order they appear in the file. Values that span several bytes, such as 32-bit integers in a binary header, depend on the byte order of the producing machine, and od has options for the common widths when you need them. For this unit, the bytes you read are the bytes on disk, and that is the fact to rely on.

Practice in the lab

The lab creates a gzip file and asks you to identify it with the file utility. Then it asks you to save the first three bytes in hexadecimal. The checker reads the saved output to confirm you looked at the signature rather than guessing from the name.

Characters to bytes: UTF-8 and line endings

Visualizer

Files hold bytes. See how a code point becomes UTF-8 bytes, and why a Windows line ending breaks shell scripts.

1 / 4
character
é
code point
U+00E9
decimal
233

Unicode gives é the number 0xE9. It is above 0x7F, so it cannot be a single ASCII byte.

printf "\xc3\xa9\n" | od -An -tx1 -c
Terminal lab · 2 steps · real Linux in your browser

Identify a gzip file by its bytes

Ask file for the type of an unknown file, then read its first three bytes.

01
Identify the type
Save the report that file gives for data.gz: ``bash file data.gz > type.txt ``
Show hint for step 1
file reads the first bytes of the file, not its name.
02
Read the signature bytes
Save the first three bytes of data.gz in hexadecimal, without an address column: ``bash od -An -tx1 -N3 data.gz > magic.txt ``
Show hint for step 2
-N3 limits the read to three bytes and -tx1 prints each as hexadecimal.
Check yourself Question 1 of 4
Concept Check What file relies on recall

On what does the file utility mainly base its guess of a file type?

Recap & Key Takeaways

File extensions are hints; file identifies a type from magic numbers and structure in the first bytes.
od prints raw bytes in octal, hexadecimal, or character form, and -N limits how many bytes it reads.
The gzip signature starts with 1f 8b 08, which is a good example of reading a magic number yourself.
Inspect unknown files with file and od before you run them or open them in an editor.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue