Unlike operating systems that require an “extension” like .doc or .txt to identify file types and what application should open them, Linux systems generally don’t. Some applications do: C compilers, for instance, expect source filenames ending with .c. But the kernel doesn’t enforce this. Instead of filename extensions, Linux uses a system of magic numbers: the first few bytes stored in an executable file tell how to run it. There’s no need to name a shell script foo.sh; plain foo is fine. (If you need to know, the command file foo will usually tell you.) This system of identifying a file type by its content isn’t used only for executable files. It’s common in many other types of files, as we’ll see. How is this all defined? And, armed with this knowledge, what can you do with it? Let’s dig in.
Checking File Types with file(1)
Over the years, there’ve been several versions of the file utility. We’ll look at the version that uses external data files named magic to characterize files. When you pass file one or more filenames, it starts by checking the file’s inode. If the file is a directory, symbolic link, or other special file, it will tell you so:
$ file /dev/tty /tmp /vmlinuz /dev/tty: character special (5/0) /tmp: sticky directory /vmlinuz: symbolic link to `boot/vmlinuz-2.4.26′
Otherwise, file looks into the file, comparing the file contents to patterns in the magic files:
$ file at autoconf autoscan at: setuid ELF 32-bit…
Please log in to view this content.
Not Yet a Member?
Register with LinuxMagazine.com and get free access to the entire archive, including: