Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get MIME file type

Status
Not open for further replies.

spookie

Programmer
May 30, 2001
655
IN
Hi,
I want to determine mime file type of files like .doc, .pdf, .rtf etc.
Now i can't go on with the extension as some extensions are wrong.
e.g. doc file having extension .rtf etc.
I guess 'file' command does that but its not giving me correct output.
Can anyone tell me how to use the command to get the result?

Thanks

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
I can't think of anything better than file to do this. What kind of incorrect output is it giving you?

The only other way would be to know the exact format of the file types you are interested in and test for correct headers and so-on.

Annihilannic.
 
Hi Annihilannic,
Thanks for the reply.

The 'file' command gives me 'ASCII text' for all formats.
The idea is to diffterntiate between .rtf and .doc etc.

The other way you suggested: how do i check the headers of a file?

Thanks

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Just look at the first few bytes of each file and see which ones are constant for a particular file type. For example, PDF files always seem to begin with the bytes 25 50 44 46 2d 31 2e in hex (obtained using head -c7 | od -t x1). This corresponds to the string "%PDF-1.". Probably best to test for %PDF- only since the version might increase in future.

Annihilannic.
 
Actually, I just noticed that Solaris' head command doesn't support -c, in which case you may need to use od -t x1 filename | head -1 or similar and examine the characters you are interested in.

For e.g.

Code:
HEADER=`od -t x1 filename | head -1`
if echo $HEADER | grep '0000000 25 50 44 46 2d' > /dev/null
then
   echo The file is a PDF
fi

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top