Most magic numbers aren't readable unless you use a tool such as od to examine the file's contents. The exception is the shebang line that we use in scripts to identify the shell to be used when they are run. This serves as a kind of magic number on its own and is used by the file command as such.
For most files, lines in the /usr/share/file/magic file describe what the file command needs to look for. The line for a PNG file, for example, looks like this:
0 string \x89PNG PNG image data,
This line tells us that there's no offset (the magic number appears at the beginning of the file),
Looking at tux.png, an image of the beloved penguin, file tells us:
$ file tux.png tux.png: PNG image data, 400 x 479, 8-bit/color RGBA, non-interlaced
Now that's a lot more than just the magic number indicates. The file command, once determining that the file is a PNG file, can look at other content in the file to determine its size and color depth.
$ od -bc tux.png | head -4 0000000 211 120 116 107 015 012 032 012 000 000 000 015 111 110 104 122 211 P N G \r \n 032 \n \0 \0 \0 \r I H D R 0000020 000 000 001 220 000 000 001 337 010 006 000 000 000 176 261 216 \0 \0 001 220 \0 \0 001 337 \b 006 \0 \0 \0 ~ 261 216
PNG files begin with this 8-byte signature which is very obvious in the output shown above.
\211 P N G \r \n \032 \n
Magic numbers help ensure that Unix systems have more to go on than file names when trying to identify file content. Examine the /usr/share/file/magic on your Linux system and you'll notice that there's a lot of information there about different types of files.
Read more of Sandra Henry-Stocker's Unix as a Second Language blog and follow the latest IT news at ITworld, Twitter and Facebook.