File Systems: The Interface

a file type

A file is just a stream of bytes, so how does the system know whether those bytes are a song, a picture, or a program? The byte 65 could be the letter A in text, a tiny part of a sound wave, or one machine instruction. The file type is the answer to "what KIND of thing are these bytes meant to be read as?" It tells programs and the OS how to interpret the contents and which application should open them.

There are two common ways to recognise a type. The first is by the file-name extension: the few letters after the last dot, like .txt, .jpg, or .exe. This is a hint stored in the name, a convention that is easy for people to see but trivial to fake, since renaming a file does not change its bytes. The second is by looking inside the file at a magic number, a fixed signature in the first few bytes that real files of a given format always start with. A deeper distinction underneath all of this is text versus binary: a text file is bytes meant to be read as human-readable characters (you can print it to a terminal), while a binary file is bytes meant for a program (printing it gives garbage).

Why it matters: getting the type right is what makes double-clicking a file launch the correct app, and getting it wrong is a real security hazard. An attacker can name a program photo.jpg.exe so the eye sees a harmless image; trusting the extension blindly is dangerous. The honest rule is that the extension is only a polite suggestion. The bytes inside (the magic number) are the trustworthy evidence of what a file really is.

Rename song.mp3 to song.txt and your music player may refuse it, but the bytes are unchanged. A tool that checks the magic number still sees the MP3 signature inside and knows it is really audio, no matter what the extension claims.

The extension is a hint in the name; the real evidence is the bytes inside.

Never trust an extension for security decisions: it is part of the name, not the contents, and is trivial to change. The magic number is far more reliable for telling what a file actually is.

Also called
file format kind檔案種類