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.
cd /workspace
printf 'hello\n' | gzip > data.gz
file data.gzThe 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.
cd /workspace
od -An -tx1 -N3 data.gzThe 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.
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.