x
Loading
 Loading
Hello, Guest | Login | Register

It’s (Not) Magic

Learn the practical magic of magic numbers.

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:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. KDE 4.4: Does It Work Yet?
  2. Writing Custom Nagios Plugins with Python
  3. Power Up Linux GUI Apps
  4. Tweeting from the Command Line with Twyt
  5. When Memory Serves You: Using ramfs and tmpfs
Follow Linux Magazine
Rackspace