views:

283

answers:

5

there is a magic number associated with each binary file , does anyone know how to retrieve this information from the file?

+1  A: 

The unix file command uses magic number. see the file man page for more.(and where to find the magic file )

ghostdog74
+1  A: 

Read this: http://linux.die.net/man/5/magic

It's complex, and depends on the specific file type you're looking for.

S.Lott
+1  A: 
file <file_name>

magic numbers are usually stored in (linux):

/usr/share/file/magic

also check this link, someone was trying to use libmagic to get the information in C program, might be useful if you're writing something yourself.

pulegium
+1  A: 

There is a file command which in turn uses a magic library, the magic library reads from a file found in /etc called magic (this is installation dependant and may vary), which details what are the first few bytes of the file and tells the file what kind of a file it is, be it, jpg, binary, text, shell script. There is an old version of libmagic found on sourceforge. Incidentally, there is a related answer to this here.

Hope this helps, Best regards, Tom.

tommieb75
+1  A: 

Use libmagic from the file package to try and sniff out the type of file if that's your goal.

There are no general "magic" numbers in binary files on unix, though different formats might define their own. The above library knows about many of those and also use various other heuristics to try and figure out the format/type of file.

nos